2017-08-30 40 views
0

我正在使用Entity Framework 6項目開發WEB API 2,並且在更新數據庫後出現問題。實體框架中的'update-database'後出錯6

錯誤:

Introducing FOREIGN KEY constraint 'FK_dbo.Rates_dbo.Users_Id_User' on table 'Rates' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints. 
Could not create constraint or index. See previous errors. 

這是我率等級:

public class Rate 
    { 
     [Key] 
     [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
     public int Id_Rate { get; set; } 
     public int Id_User { get; set; } 
     public int Id_Recipe { get; set; } 
     public int Value_Rate { get; set; } //1-5 

     public virtual User User { get; set; } 
     public virtual Recipe Recipe { get; set; } 
    } 

和我的用戶等級:

public class User 
{ 
    [Key] 
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
    public int Id_User { get; set; } 
    public int Id_List_Products { get; set; } 
    public int Id_List_Black_Products { get; set; } 
    public string Login { get; set; } 
    public string Email { get; set; } 
    public string Password { get; set; } 
    public Boolean Social_Account { get; set; } // 1 - social account, 0 - normal account 
    public string URL_Avatar { get; set; } //URL of avatar thumbnail 

    public virtual List_Products List_Products { get; set; } 
    public virtual List_Black_Products List_Black_Products { get; set; } 
} 

我不知道在哪裏的問題。 任何提示?

+0

安置自己的ConnectionString Model類..一個包含'modelBuilder' –

+0

您需要指定應該發生什麼給用戶刪除費率。如果您有用戶參考速率和速率參考用戶,級聯刪除會導致問題。 –

+0

[實體框架:如何解決「FOREIGN KEY約束可能導致循環或多個級聯路徑」?](https://stackoverflow.com/questions/14489676/entity-framework-how-to-solve-foreign-鍵約束-MAY-原因週期 - 或多) –

回答

1

由於關係的多個路徑發生此問題。如果您刪除了一條記錄,它將最終從另一個表中刪除其他記錄,這將最終從另一個表中刪除更多記錄,最終從您開始的表中刪除記錄,循環將重複。真正的災難性的,開發商最糟糕的噩夢。爲了解決這個問題,添加自定義屬性,例如,在遷移:

CreateTable(
      "dbo.SomeTable", 
      c => new 
      { 
       id = c.Int(nullable: false, identity: true), 
       createdon = c.DateTime(), 
       createdby = c.String(), 
      }) 
      .PrimaryKey(t => t.id) 
      .ForeignKey("dbo.Customers", t => t.Customerid, cascadeDelete: true) 
      .ForeignKey("dbo.AnotherTable", t => t.Anotherid, cascadeDelete: false) 
      .Index(t => t.Customerid) 
      .Index(t => t.Scopeid); 

設置cascadeDeletefalse或受影響的表/(模型類背襯)的AutoUpdatefalse並相應更新數據庫。

簡單地說,你們的關係是循環的,例如: 客戶 - >工資 - >工資單 - >客戶 所以,當你刪除一個客戶,其相應的薪酬結果是/刪除其刪除受到尊重的工資單記錄將循環回刪除鏈接的客戶,並且此循環不斷重複導致混亂。所以Entity Framework和SQL很好地理解了這一點,並提示用戶關閉受影響/導致錯誤的刪除/更新連接。

0

歐凱,我改變cascadeDelete爲false:

CreateTable(
      "dbo.Rates", 
      c => new 
       { 
        Id_Rate = c.Int(nullable: false, identity: true), 
        Id_User = c.Int(nullable: false), 
        Id_Recipe = c.Int(nullable: false), 
        Value_Rate = c.Int(nullable: false), 
       }) 
      .PrimaryKey(t => t.Id_Rate) 
      .ForeignKey("dbo.Recipes", t => t.Id_Recipe, cascadeDelete: true) 
      .ForeignKey("dbo.Users", t => t.Id_User, cascadeDelete: false) 
      .Index(t => t.Id_User) 
      .Index(t => t.Id_Recipe); 

和它的作品;)