问题
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