SQL约束异常

编程入门 行业动态 更新时间:2024-10-23 09:36:19
SQL约束异常 - Xamarin移动应用IOS(SQL Constraints Exception - Xamarin Mobile app IOS)

我有一个问题将我推上了墙。

下面的代码将数据添加到数据库中,然后列出。 问题是,它添加第一个音符非常好,然后我去添加另一个音符,我得到一个“约束”的错误! 我只能看到为什么。

模态;

namespace MyApp.Models { [Table("notes")] public class Note { [PrimaryKey, AutoIncrement] public int Id { get; set; } [Unique] public long ProgramId { get; set; } public string source { get; set; } public int TaskId { get; set; } public string UserId { get; set; } public DateTime DateAdded { get; set; } public string Content { get; set; } public static void addNote(Note newNote) { //SQLiteConnection conn = new SQLiteConnection(AssessNETApp.Data.Globals.DB); using (var conn = new SQLiteConnection(AssessNETApp.Data.Globals.DB)) { try { conn.Insert(newNote); } catch (SQLiteException ex) { System.Diagnostics.Debug.WriteLine(ex.Message); } } }

按钮单击代码以添加注释

async void OnAddNoteButtonClick(object sender, EventArgs e) { if (Text.Text.Length > 0) { Note NoteData = new Note(); NoteData.DateAdded = DateTime.Now; NoteData.TaskId = thisTask.TaskId; NoteData.UserId = MyApp.Data.LoggedInUser.UserID; NoteData.Content = Text.Text.ToString(); Note.addNote(NoteData); await Navigation.PopAsync(); } }

Modal;

namespace MyApp.Models { [Table("notes")] public class Note { [PrimaryKey, AutoIncrement] public int Id { get; set; } [Unique] public long ProgramId { get; set; } public string source { get; set; } public int TaskId { get; set; } public string UserId { get; set; } public DateTime DateAdded { get; set; } public string Content { get; set; } public static void addNote(Note newNote) { //SQLiteConnection conn = new SQLiteConnection(AssessNETApp.Data.Globals.DB); using (var conn = new SQLiteConnection(AssessNETApp.Data.Globals.DB)) { try { conn.Insert(newNote); } catch (SQLiteException ex) { System.Diagnostics.Debug.WriteLine(ex.Message); } } }

Code for button click to add note

async void OnAddNoteButtonClick(object sender, EventArgs e) { if (Text.Text.Length > 0) { Note NoteData = new Note(); NoteData.DateAdded = DateTime.Now; NoteData.TaskId = thisTask.TaskId; NoteData.UserId = MyApp.Data.LoggedInUser.UserID; NoteData.Content = Text.Text.ToString(); Note.addNote(NoteData); await Navigation.PopAsync(); } }

First note i add , fine! Second, error..

最满意答案

[Unique] public long ProgramId { get; set; }

如果你没有明确指定一个ProgramID,它将默认为0.所以第一个是Unique,但第二个也是0,这违反了约束。

[Unique] public long ProgramId { get; set; }

if you don't explicitly assign a ProgramID, it will default to 0. So the first is Unique, but the second is also 0, which violates the constraint.

更多推荐

本文发布于:2023-07-22 10:01:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1219182.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:异常   SQL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!