首页 > 解决方案 > 影响 EF CodeFirst 迁移的数据库更改

问题描述

我有一个使用 EF Code First 的项目。我通过将一些约束更改为“删除级联”来更改 DBMS 中的数据库,然后我立即撤消了更改。

当我运行我的代码时,我得到了这个错误:Unable to update database to match the current model because there are pending changes and automatic migration is disabled.所以我运行了add-migration newconstraints它并生成了迁移文件。

DropForeignKey("dbo.UserPane", "ID", "dbo.UserIndicatorPane");
DropForeignKey("dbo.UserAxisPane", "ID", "dbo.UserPane");
DropForeignKey("dbo.UserPaneProperty", "UserPaneID", "dbo.UserPane");
DropForeignKey("dbo.UserIndicator", "UserIndicatorPaneID", "dbo.UserIndicatorPane");
DropIndex("dbo.UserPane", new[] { "ID" });
DropPrimaryKey("dbo.UserPane");
DropPrimaryKey("dbo.UserIndicatorPane");
AlterColumn("dbo.UserPane", "ID", c => c.Int(nullable: false, identity: true));
AlterColumn("dbo.UserIndicatorPane", "ID", c => c.Int(nullable: false));
AddPrimaryKey("dbo.UserPane", "ID");
AddPrimaryKey("dbo.UserIndicatorPane", "ID");
CreateIndex("dbo.UserIndicatorPane", "ID");
AddForeignKey("dbo.UserIndicatorPane", "ID", "dbo.UserPane", "ID");
AddForeignKey("dbo.UserAxisPane", "ID", "dbo.UserPane", "ID");
AddForeignKey("dbo.UserPaneProperty", "UserPaneID", "dbo.UserPane", "ID", cascadeDelete: true);
AddForeignKey("dbo.UserIndicator", "UserIndicatorPaneID", "dbo.UserIndicatorPane", "ID", cascadeDelete: true);

现在update-database给我这个错误:The constraint 'PK_dbo.UserPane' is being referenced by table 'UserIndicatorPane', foreign key constraint 'FK_dbo.UserIndicatorPane_dbo.UserPane_ID'. Could not drop constraint. See previous errors.

我不确定如何修复此错误,因为我没有进行任何永久性更改。

标签: sqlentity-framework

解决方案


我想到了。通过删除 FKFK_dbo.UserIndicatorPane_dbo.UserPane_ID和 DBMS 中的索引,我能够运行更新数据库。

https://stackoverflow.com/a/42267673/802331


推荐阅读