0

I have a project that uses EF Code First. I changed my database inside DBMS by altering a few constraints to 'on delete cascade' then I immediately undid to changes.

When I ran my code, I got this error: Unable to update database to match the current model because there are pending changes and automatic migration is disabled. So I ran the add-migration newconstraints and it generated the migration file.

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);

Now update-database gives me this error: 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.

I'm not sure how to fix this error since I didn't make any permanent changes.

Mike
  • 2,299
  • 13
  • 49
  • 71

1 Answers1

0

I figured it out. By deleting the FK FK_dbo.UserIndicatorPane_dbo.UserPane_ID and the index in DBMS I was able to run update-database.

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

Mike
  • 2,299
  • 13
  • 49
  • 71