2014-05-06 31 views
1

我創建了一些新表,我注意到我沒有使用好的命名約定的ID。無法更新數據庫,因爲實體無法刪除約束

所以我做了一個新的add-migration與新的ID,但是當我嘗試update-database我有以下錯誤:

「PK_dbo.DB_User_Type」是由表 「AspNetUsers」引用的約束,外鍵約束 'FK_dbo.AspNetUsers_dbo.DB_User_Type_DB_User_Type_Id'。無法下降 約束。查看以前的錯誤。

我不明白,因爲腳本以刪除所有約束開始。

有人可以解釋一下如何解決這個錯誤嗎?

我正在使用實體6.1的新方法code first from an existing database。這裏是Up功能

public override void Up() 
     { 
      DropForeignKey("dbo.DB_Company_Profile", "DB_Category_Id", "dbo.DB_Category"); 
      DropForeignKey("dbo.DB_Category_Translation", "DB_Category_Id", "dbo.DB_Category"); 
      DropForeignKey("dbo.DB_User_Type_Translation", "DB_User_Type_Id", "dbo.DB_User_Type"); 
      DropForeignKey("dbo.DB_Company_Profile", "category_id", "dbo.DB_Category"); 
      DropForeignKey("dbo.DB_Category_Translation", "category_id", "dbo.DB_Category"); 
      DropForeignKey("dbo.AspNetUsers", "DB_User_Type_user_type_id", "dbo.DB_User_Type"); 
      DropForeignKey("dbo.DB_User_Type_Translation", "user_type_id", "dbo.DB_User_Type"); 
      DropIndex("dbo.DB_Company_Profile", new[] { "DB_Category_Id" }); 
      DropIndex("dbo.DB_Category_Translation", new[] { "DB_Category_Id" }); 
      DropIndex("dbo.DB_User_Type_Translation", new[] { "DB_User_Type_Id" }); 
      DropColumn("dbo.DB_Company_Profile", "category_id"); 
      DropColumn("dbo.DB_Category_Translation", "category_id"); 
      DropColumn("dbo.DB_User_Type_Translation", "user_type_id"); 
      RenameColumn(table: "dbo.AspNetUsers", name: "DB_User_Type_Id", newName: "DB_User_Type_user_type_id"); 
      RenameColumn(table: "dbo.DB_Company_Profile", name: "DB_Category_Id", newName: "category_id"); 
      RenameColumn(table: "dbo.DB_Category_Translation", name: "DB_Category_Id", newName: "category_id"); 
      RenameColumn(table: "dbo.DB_User_Type_Translation", name: "DB_User_Type_Id", newName: "user_type_id"); 
      RenameIndex(table: "dbo.AspNetUsers", name: "IX_DB_User_Type_Id", newName: "IX_DB_User_Type_user_type_id"); 
      DropPrimaryKey("dbo.DB_Category"); 
      DropPrimaryKey("dbo.DB_User_Type"); 
      AddColumn("dbo.DB_Category", "category_id", c => c.Int(nullable: false, identity: true)); 
      AddColumn("dbo.DB_User_Type", "user_type_id", c => c.Int(nullable: false, identity: true)); 
      AlterColumn("dbo.DB_Company_Profile", "category_id", c => c.Int(nullable: false)); 
      AlterColumn("dbo.DB_Category_Translation", "category_id", c => c.Int(nullable: false)); 
      AlterColumn("dbo.DB_User_Type_Translation", "user_type_id", c => c.Int(nullable: false)); 
      AddPrimaryKey("dbo.DB_Category", "category_id"); 
      AddPrimaryKey("dbo.DB_User_Type", "user_type_id"); 
      CreateIndex("dbo.DB_Company_Profile", "category_id"); 
      CreateIndex("dbo.DB_Category_Translation", "category_id"); 
      CreateIndex("dbo.DB_User_Type_Translation", "user_type_id"); 
      AddForeignKey("dbo.DB_Company_Profile", "category_id", "dbo.DB_Category", "category_id", cascadeDelete: true); 
      AddForeignKey("dbo.DB_Category_Translation", "category_id", "dbo.DB_Category", "category_id", cascadeDelete: true); 
      AddForeignKey("dbo.DB_User_Type_Translation", "user_type_id", "dbo.DB_User_Type", "user_type_id", cascadeDelete: true); 
      AddForeignKey("dbo.AspNetUsers", "DB_User_Type_user_type_id", "dbo.DB_User_Type", "user_type_id"); 
      DropColumn("dbo.DB_Category", "Id"); 
      DropColumn("dbo.DB_User_Type", "Id"); 
     } 
+0

如果你沒有數據,最乾淨的方法是刪除DB> – DarthVader

+0

然後它說它不存在。我想我不得不做另外一些比更新數據庫 – Marc

+0

我想我可以針對另一個遷移,但我想知道是否有一些不那麼複雜 – Marc

回答

0

嗯,大概有外鍵的一些規則 - 他們中的一些是爲了處理這種情況,當您嘗試刪除具有與之相關聯的另一個記錄的記錄。 就你而言,你似乎想要將CASCADE規則應用於你的關係。這樣的實體將刪除所有FKS

+0

我有這個錯誤是否正常,那麼如果沒有數據? – Marc

+0

你能提供一個實體的例子嗎? – Marc

+0

爲了執行CASCADE,您需要修改建立PK-FK關係的表。你需要修改約束本身。 – Andrew

-1
  1. DropForeignKey( 「dependantTable」, 「dependantColumn」, 「principalTable」)
  2. DropPrimaryKey( 「principalTable」)
  3. AddPrimaryKey( 「principalTable」, 「principalColumn」 )
  4. AddForeignKey( 「dependantTable」, 「dependantColumn」, 「principalTable」)
0

我有同樣的問題。 This post helped me.

問題是EF沒有自動重命名我的FK_CONSTRAINST。因此DropForeignKey()不正確。你可以通過手動SQL查詢來解決這個問題(查看上面的帖子)。但我通過首先在數據庫中手動更改FK_CONSTRAINST來解決此問題。請記住,您可以使用-Verbose檢查查詢。它更容易調試。

0

我必須在運行update-database命令之前手動刪除外鍵。因爲我的數據庫中的約束名稱像'FK_dbo.AspNetUsers_dbo.DB_User_Type_DB_User_TypeId'一樣存在。
儘管腳本嘗試刪除的約束爲'FK_dbo.AspNetUsers_dbo.DB_User_Type_DB_User_Type_Id',因此爲 。