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.
更多推荐
发布评论