Error while doing Migrations EF core 2.0, changing

2019-06-20 13:45发布

问题:

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:

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.