Changing Primary Key using Entity Framework Core Migrations

≯℡__Kan透↙ 提交于 2019-12-22 06:36:05

问题


I am trying to Change the Primary key of a table via Entity Framework Core Migrations:

protected override void Up(MigrationBuilder migrationBuilder)
{
        migrationBuilder.DropPrimaryKey(
            name: "PK_Permissions",
            table: "Permissions");
}

When I try to update the database I get the following error message:

To change the IDENTITY property of a column, the column needs to be dropped and recreated.

How can I update the database?


回答1:


I have found a solution: This seems to be a bug in EF Core 1.1

I changed the Migration file.

More Information about this can be found here: https://thisworksonmymachine.com/2017/02/13/ef-core-the-setup-part-4/




回答2:


It's always a risky idea to manipulate the migrations... The simplest way is to clean the slate, if possible: Take the table out from the DBContext (this will drop the table) Create Migration and update database Restore the table in the context and do your changes. Create Migration and update database.

Obviously, you need to consider backup and restore of data and constraints...




回答3:


On attempting to get IdentityServer4 EF migrations (SQLServer) I had this same problem. Updating Microsoft.EntityFrameworkCore to v2.1.3 and Microsoft.EntityFrameworkCore.Tools to v2.1.3 seemed to fix the issue.




回答4:


The following is not a solution for everyone. Namely, if you have an established database in multiple environments.

If you don't care about your migrations, simply start from scratch by deleting all of your migrations. Then recreate the first one with your new schema. Should work just fine.



来源:https://stackoverflow.com/questions/43798708/changing-primary-key-using-entity-framework-core-migrations

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