Entity Migration. Could not drop constraint. See previous errors

c# ef-migrations entity-framework

Question

Here is the code that produce this error. I've tried solution for renaming the AddForeignKey but the error is same.

'PK_dbo.Item' is not a constraint.
Could not drop constraint. See previous errors.

Can you suggest some solution?

public override void Up()
{
    DropForeignKey("dbo.AddGallery", "item_fk_id", "dbo.Item");
    DropForeignKey("dbo.ExtraFieldValue", "item_fk_id", "dbo.Item");
    DropPrimaryKey("dbo.Item");
    AddColumn("dbo.Item", "id", c => c.Int(nullable: false, identity: true));
    AddPrimaryKey("dbo.Item", "id");
    AddForeignKey("dbo.AddGallery", "item_fk_id", "dbo.Item", "id");
    AddForeignKey("dbo.ExtraFieldValue", "item_fk_id", "dbo.Item", "id");
    DropColumn("dbo.Item", "item_id");
}

public override void Down()
{
    AddColumn("dbo.Item", "item_id", c => c.Int(nullable: false, identity: true));
    DropForeignKey("dbo.ExtraFieldValue", "item_fk_id", "dbo.Item");
    DropForeignKey("dbo.AddGallery", "item_fk_id", "dbo.Item");
    DropPrimaryKey("dbo.Item");
    DropColumn("dbo.Item", "id");
    AddPrimaryKey("dbo.Item", "item_id");
    AddForeignKey("dbo.ExtraFieldValue", "item_fk_id", "dbo.Item", "id");
    AddForeignKey("dbo.AddGallery", "item_fk_id", "dbo.Item", "id");
}
1
4
10/9/2017 10:29:08 PM

Accepted Answer

I believe the primary key no longer exists and this is why it cannot be dropped.

2
5/25/2015 1:04:28 AM

Popular Answer

I find an solution for this problem. Because I've trying to change the name of the existing primary key name, I encountered an error while migration because that primary key was referenced to other tables. So the solution was simply to add [Column("id")] DataAnnotation and than make the migration. Column name of the primary key was successfully changed without any error.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow