Search code examples
sql-serverdatabaseasp.net-core-webapiasp.net-identity

The AspNetUserRoles table was cleared during migrations


I'm developing a website with ASP.NET Core 8 Web API, SQL Server, utilizing the identity framework. The database contains various tables such as AspNetUsers, AspNetRoles, AspNetUserRoles, Brands, and more. Since I have already populated the database with real data, I prefer not to drop the table. When I need to make modifications to the database, I typically use command lines. However, each time I update the table, the AspNetUserRoles table gets cleared. How can I preserve the data in the userRoles table?

Here are the command lines I tried.

dotnet ef migrations add updateUserTable

dotnet ef database update

There is userRoles table after I execute these command lines but the userRoles table is empty. I'm using VSCode for developing tool.

enter image description here


Solution

  • You must create a new migration to incrementally update the database schema whenever you make a change to the model. Run the command below:

    dotnet ef migrations add AddNewMig - This command creates a new migration called AddNewMig which EF should recognise is an update to the exsiting tables (and therefore the migration should only be and update)

    dotnet ef database update AddNewMig - This will then run the AddNewMig migration

    Reference: Migrations Overview