无法更新数据库,因为实体无法删除约束
本文关键字:删除 约束 实体 更新 数据库 因为 | 更新日期: 2023-09-27 18:02:14
我创建了一些新表,我注意到我没有为id使用良好的命名约定。
所以我用新的id做了一个新的add-migration
,但是当我尝试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");
}
我也遇到了同样的问题。这篇文章帮助了我。
问题是EF没有自动重命名我的FK_CONSTRAINST
。因此,DropForeignKey()
是不正确的。您可以通过手动SQL查询来解决这个问题(请参阅上面的帖子)。但我通过首先在DB中手动更改FK_CONSTRAINST
来解决它。记住,您可以使用-Verbose
来检查查询。
我必须在运行update-database
命令之前手动删除外键。因为我的DB中的约束名称类似于:FK_dbo.AspNetUsers_dbo.DB_User_Type_DB_User_TypeId
当脚本试图删除的约束是:FK_dbo.AspNetUsers_dbo.DB_User_Type_DB_User_Type_Id
关于外键有一些规则——其中一些规则是用来处理这样的情况:当您试图删除一条记录时,另一条记录与它相关联。在你的情况下,你似乎想把CASCADE
规则应用到你的关系中。这样,实体将删除其所有fk
-
DropForeignKey("dependantTable", "dependantColumn", "principalTable")
-
DropPrimaryKey("principalTable")
-
AddPrimaryKey("principalTable", "principalColumn",)
-
AddForeignKey("dependantTable", "dependantColumn", "principalTable")