Error while doing Migrations EF core 2.0, changing Identity id from string to int

asp.net-core-mvc ef-migrations entity-framework-core identity migration

Question

Scenario:

I have received auto generated project in ASP.NET CORE from my colleague. There is auto-generated code for Account/Manage service. This code includes ApplicationUser Class, DBContext and migration folder with 00000000000000_CreateIdentitySchema.cs and 20180323155805_Snapshot.cs. I have been trying to change my User class to have integer id. To do that I added generic to IdentityUser:

public class ApplicationUser : IdentityUser**<int>**
{
}

I also had to create ApplicationRole class, because before it had been created in migration files.

public class ApplicationRole : IdentityRole<int>
{
}

I also changed my context:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser, 
    **ApplicationRole, int**>

In migration files, there is created login scheme. After my changes addition, I add new migrations. During adding migration i received this error:

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

1
7
3/23/2018 6:46:34 PM

Accepted Answer

You'll need to update the generated migration to do it in steps. Replace the AlterColumn call with the following operations:

  1. Add the new column with a temporary name
  2. Drop the original column
  3. Rename the new column using the original columns name

You may also have to rebuild (drop and re-create) all the constraints that reference the column.

It's not trivial, which is why EF currently doesn't handle it. Feature request #329 is about updating EF to handle this automatically.

7
3/27/2018 3:54:24 PM


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