实体框架和SQLite的奇怪错误

时间:2017-09-27 03:41:13

标签: c# entity-framework

当我尝试更新我的数据库时,它失败并显示错误

  

{“约束失败\ r \ nUNUNQUE约束失败:Accounts.AccountID”}

奇怪的是,如果我清空所有记录的数据库,那么它将工作一次,下一次失败。 这是代码:

//parameter is the current view model that is displayed
public void Execute(object parameter)
{
    using (var db = new mainEntities())
    {
        var vm = (NewAccountViewModel)parameter;
        decimal balance = 0;
        decimal.TryParse(vm.Balance, out balance);

        db.Accounts.Add(new Account() {
                    FirstName = vm.FirstName,
                    LastName = vm.LastName,
                    Balance = balance
                });

        db.SaveChanges();
    }
}

public partial class Account
{
    public long AccountID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public decimal Balance { get; set; }
}

我使用VS SQLite Toolbox扩展来创建表,它生成的SQL代码是:

CREATE TABLE [Accounts] (
  [AccountID] INTEGER NOT NULL
, [FirstName] TEXT NOT NULL
, [LastName] TEXT NOT NULL
, [Balance] NUMERIC NOT NULL
, CONSTRAINT [sqlite_master_PK_Accounts] PRIMARY KEY ([AccountID])
, CONSTRAINT [PK_Accounts] PRIMARY KEY ([AccountID])
);

0 个答案:

没有答案