2017-10-25 8 views
0

context.AddとSaveを実行しようとすると、次のクラスでこのエラーが発生します。EFコアSQLIte一意制約が失敗しました

$ 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顧客です:検査

私は、DBをヌックにして、非仮想にカスタマオブジェクトを変更し、すべての顧客のプロパティを削除しました(ナビゲーションを除く)、いくつかの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); 
      }); 

答えて

0

です。デルフトについての話...

public WriteResult CreateInspection(Customer customer) 
{ 
    SalesforceId = customer.SalesforceId; 
    CustomerId = customer.Id; 
    //Customer = customer; 
    return InspectionService.CreateInspection(this); 
} 
関連する問題