EF Core SQLIte唯一约束失败

时间:2017-10-25 04:20:21

标签: c# entity-framework sqlite

尝试执行context.Add和Save时,我在以下类中遇到此错误。

  

$ exception {Microsoft.EntityFrameworkCore.DbUpdateException:错误   更新条目时发生。查看内部异常   细节。 ---> Microsoft.Data.Sqlite.SqliteException:SQLite错误19:   'UNIQUE约束失败:Customer.Id'。

 public class Customer
{
    public int Id { get; set; }
    public string SalesforceId { get; set; }
    public string CustomerName { get; set; }
    public string LocationName { get; set; }
    public string StreetAddress { get; set; }
    public string StreetAddress2 { get; set; }
    public string City { get; set; }
    public State StateName { get; set; }
    public string ZipCode { get; set; }
    public virtual ICollection<Inspection> Inspections { get; set; }


    public WriteResult Create(Customer customer)
    {
        return CustomerService.CreateNewCustomer(customer);
    }
}

public class Inspection
{
    public Guid Id { get; set; }
    public DateTime LastUpdatedDate { get; set; }
    public int CustomerId { get; set; }
    public string SalesforceId { get; set; }
    public bool Synced { get; set; } 
    public bool Archived { get; set; }

   public virtual ICollection<InspectionItem> InspectionItems{ get; set; }

    public virtual Customer Customer { get; set; }

    public WriteResult CreateInspection(Customer customer)
    {
        SalesforceId = customer.SalesforceId;
        CustomerId = customer.Id;
        Customer = customer;
        return InspectionService.CreateInspection(this);
    }

    public WriteResult CreateInspection()
    {
        return InspectionService.CreateInspection(this);
    }

作家

 public static WriteResult CreateInspection(Inspection inspection)
    {
        try
        {
            inspection.LastUpdatedDate = DateTime.Now;
            context.Inspections.Add(inspection);
            context.SaveChanges();
            return DataWrite.Success;
        }
        catch (Exception ez)
        {
            return WriteResult.ErrorOnWrite;
        }
    }

public static WriteResult CreateNewCustomer(Customer customer)
    {
        try
        {
            if (context.Customers.FirstOrDefault(c => c.SalesforceId == customer.SalesforceId) != null)
                return WriteResult.DuplicateKey;

            context.Customers.Add(customer);
            context.SaveChanges();
            return WriteResult.Success;
        }
        catch (Exception e)
        {
            return WriteResult.ErrorOnWrite;
        }
    }

有2个工作流程,用户可能会也可能不会输入客户。无论结果如何,都必须在那时创建新的检查。当没有客户与检查相关联时,一切正常 - 它们都可以独立创建并且很好。只有在检查引用了客户时才会出现此问题。期望的关系是1:M客户:检查

我尝试过修改数据库,将客户对象更改为非虚拟,删除所有客户属性(导航除外),重新阅读关于关系和导航的一些MSDN文章,但无法提出解决这个问题。

任何人都有任何关于我必须完全俯视的指示吗?

这是约束

constraints: table =>
            {
                table.PrimaryKey("PK_Survey", x => x.Id);
                table.ForeignKey(
                    name: "FK_Survey_Customer_CustomerId",
                    column: x => x.CustomerId,
                    principalTable: "Customer",
                    principalColumn: "Id",
                    onDelete: ReferentialAction.Restrict);
            });

1 个答案:

答案 0 :(得分:0)

当我保存导致问题时,导航对象包含在模型中。谈论一个derp ...

public WriteResult CreateInspection(Customer customer)
{
    SalesforceId = customer.SalesforceId;
    CustomerId = customer.Id;
    //Customer = customer;
    return InspectionService.CreateInspection(this);
}