Changing Primary Key using Entity Framework Core Migrations

21,000

Solution 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/

Solution 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...

Solution 3

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.

Solution 4

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.

Share:
21,000
Alexander
Author by

Alexander

Updated on July 09, 2022

Comments

  • Alexander
    Alexander over 1 year

    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?