我有一个问题正在推动我的问题。
下面的代码在数据库中添加了一个注释,然后列出。问题是,它添加了第一个音符非常好,然后我去添加另一个音符,我得到一个"约束"错误!我只能明白为什么。
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();
}
}
首先请注意我添加,好!第二,错误..
答案 0 :(得分:0)
[Unique]
public long ProgramId { get; set; }
如果你没有明确指定一个ProgramID,它将默认为0.所以第一个是Unique,但第二个也是0,这违反了约束。
答案 1 :(得分:0)
我已经修好了。出于某种原因,我想知道实际的数据表是否已经改变,考虑到它们仍然在设备上。我现在放弃了这些表,所以他们重新创建,因此实际上已经删除了我所拥有的[Unique]。
获得的经验教训是,如果您对模型进行了更改,则删除其他表,除非每次使用该应用程序时都创建数据库,否则它仍然只是运行原始模型。