Error while running Update-Database

北城余情 提交于 2019-12-04 07:08:14

It looks like the database can't alter/drop the ID column because another table has a foreign key referencing the ID column. If you drop the reference, update the column, and then re-add the reference, you should be good to go...

PROBLEM SOLVED: After deleting all migration related folders, files and tables, I re enable the Migrations and repopulated the database as you've said

Building on the accepted answer, here are specific steps to work around the issue without dropping all migrations or manually editing one.

In my situation I had

public class Parent
{
    public virtual ICollection<Child> A { get; set; }
}

The type for the child had to be changed from Child to OtherChild. That is when I encountered the error.

To work around it, I created two migrations. The first simply unmapped the property A from EF. I could have also commented it out, but quite a bit of code accessing the property would have failed to compile.

public class Parent
{
    [NotMapped]
    public virtual ICollection<Child> A { get; set; }
}

add-migration UnmapA

update-database

then I re-mapped the property and changed the type

public class Parent
{
    public virtual ICollection<OtherChild> A { get; set; }
}

add-migration ChangeChildType

update-database

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