Search code examples
c#asp.netasp.net-mvcdatabase-migrationentity-framework-migrations

How to add missing migrations for Identity? (Invalid object name 'dbo.AspNetRoles')


Error

I have created an asp.net application and on my development machine it created these built in tables which is necessary to use the identity part in asp.net mvc.

However, after the initial create when I had to create an SwitchToIdentityDbContext migration which had all the logic to create these tables. But in my application at the time, these tables did already exist so I removed that logic. After that (I think) it resulted in the missing tables for identity when I try to deploy my application to a new datasource that does not have these tables.

Is there a way to generate the missing migrations in order to get this running?

As of now, I have this in my config:

AutomaticMigrationsEnabled = true;
AutomaticMigrationDataLossAllowed = true;

Even if I remove that migration and add a new one, it's empty.


Solution

  • If you don't care for the data residing in your database, one option to correct the schema is to delete all your .cs migrations in Visual Studio and all records in your dbo.__MigrationHistory table in your database. After that you can try to create a brand new migration in Visual Studio to fix the database.

    However if that doesn't work, you can clear all your migrations in Visual Studio and completely obliterate the database. Then run

    add-migration CreateMyDb, then update-database

    and you should be good to go.

    If you do care for your data, you can always export the contents of your database tables to .csv files in Server Management Studio (if you use it) before destroying the migrations.