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

十年热恋 提交于 2020-01-14 07:55:29

问题


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.



来源:https://stackoverflow.com/questions/49454227/error-while-doing-migrations-ef-core-2-0-changing-identity-id-from-string-to-in

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