Entity Framework Migrations can't drop table because of foreign key constraint

会有一股神秘感。 提交于 2019-11-30 17:25:11

If the dbo.Bingo_Bonus table name has ever changed, or if any of the columns in the foreign key relationships have changed, EF does not rename the foreign key constraints automatically to match. I had a similar problem and I had to manually add a line like this because the DropForeignKey() function was not actually deleting the key it was supposed to:

Sql(@"ALTER TABLE [dbo].[MyTable] DROP CONSTRAINT [FK_dbo.Constraint_Name_From_Before_Table_Change]");

You cannot drop Bingo_Bonus table, because it still has references to Bingo_Bonus_Amount and Bingo_Bonus_Type tables, changing the order in the Up() method will solve the problem

by putting :

DropTable("dbo.Bingo_Bonus_Amount");
DropTable("dbo.Bingo_Bonus_Type");

before:

DropTable("dbo.Bingo_Bonus");

Your code will be:

 public override void Up()
    {
        DropForeignKey("dbo.Bingo_Review", "BingoID", "dbo.Bingo");
        DropForeignKey("dbo.Bingo_Review_Text", "BingoReviewID", "dbo.Bingo_Review");
        DropForeignKey("dbo.Bingo_Bonus", "BingoID", "dbo.Bingo");
        DropForeignKey("dbo.Bingo_Bonus_Amount", "BingoBonusID", "dbo.Bingo_Bonus");
        DropIndex("dbo.Bingo_Bonus", new[] { "BingoID" });
        DropIndex("dbo.Bingo_Review", new[] { "BingoID" });
        DropIndex("dbo.Bingo_Review_Text", new[] { "BingoReviewID" });
        DropIndex("dbo.Bingo_Bonus_Amount", new[] { "BingoBonusID" });
        DropTable("dbo.Bingo_Bonus_Amount");
        DropTable("dbo.Bingo_Bonus_Type");
        DropTable("dbo.Bingo_Bonus");
        DropTable("dbo.Bingo");
        DropTable("dbo.Bingo_Review");
        DropTable("dbo.Bingo_Review_Text");

    }

I was able to drop using GUI. When tried to run the query with alter , the '.' symbol was having some error displayed







易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!