Question: How do you resolve the Entity Framework Error in Asp.Net Core 3.1: To change the IDENTITY property of a column, the column needs to be dropped and recreated
Answer: First of all, make sure that your ef core is up to date by updating the dotnet tools in Powershell using the command: dotnet tool update --global dotnet-ef
- After verifying the dotnet ef core is up to date, remove the migration using Powershell.
- to do this, you need to be in the project folder. CD into the project folder and run the following command:
- dotnet ef migrations remove --context SpecifyYourDatabaseContextIfYouHaveMoreThanOne
- dotnet ef update --context SpecifyYourDatabaseContextIfYouHaveMoreThanOne
If the commands are successful, go ahead and add a migration with your changes in the class modal
- dotnet ef migrations add NameOfMigration --context SpecifyYourDatabaseContextIfYouHaveMoreThanOne
- If you get the error: Column names in each table must be unique. The column name 'id' in table 'TableName' is specified more than once.
- Make sure the column name is unique, and that the column name does not exist in the Table you are trying to update through the code-first Entity Framework.
[NB] Keep in mind how you scaffolded the first Modal, was it Database First or Code First Entity Framework you used when creating a new Class Model? This might also contribute to the error you are having.
- Make sure that the column name 'id' does not exist in the table (Database side).
- In order to check whether column name, 'id' does not exist in the table on the Database Side, right-click the table and got to "Design", this will show you whether the table contains column 'id' or not.
- [NB] If you just expand the Columns tab under the table, sometimes the table does not show you the column exists.
[Note] Read about how to optimize Entity Framework for Performance here.
Edit this Article