[Solved] There is already an object named AspNetRoles in the database. (entity-framework-core)

I have an asp.net core mvc website using entity framework core.

I don’t know how it initially happened, but I can’t get past the error:
“There is already an object named AspNetRoles in the database”

I ended up deleted the database, deleting all my migration .cs files and starting from scratch.

I’ve then tried
add-migration MyInitialMigration

same error

I then deleted the database again, and tried a direct
(without adding a migration)

still the same error

I tried changing the appsettings config string to point to a new, non-existing database.

Still the same error!

Previously on MVC5/EF6 I’ve encountered this error and have resolved it using the same method as this answer:
There is already an object named in the database

-Add-Migration Initial -IgnoreChanges

However -IgnoreChanges is unsupported in EFCore

Or previously on EF6 I’ve resolved it by adding a migration, then deleting everything in the Up/Down and running an empty migration. This has not worked on EFCore

How can I continue? I’ve checked the migration table and they’re always empty even though it’s created the whole database each time. Just doesn’t seemed to have logged it in the migrations table

Solution #1:

Found my issue.

I had followed this tutorial to load appsettings from EntityFramework:

In the EntityFrameworkConfigurationProvider there is a public override void Load() which has a dbContext.Database.EnsureCreated(); and a CreateAndSaveDefaultValues

Between the two of those it looks like it was initializing the database without tracking the migration.

I commented those out, ran a migration and it worked fine. Hopefully I don’t have to comment them out every time I want to run a migration :/

Respondent: mejobloggs

Solution #2:

This error happened after I changed my database (I stopped using MSSQLLocalDB and started using Sql Server from the production table).

Here is what I did to make it work :

  • Deleting the migrations folder
  • Commenting the last changes I wanted to apply
  • Calling the Add-Migration command : Add-Migration InitialDbChange
  • Commenting all the commands inside the Up method of the migration file created (keeping only the content of the Down method)
  • Calling the update-database command : update-database

I have a now a clean Migration from my database. Now my futur changes will be from this baseline.

Found the solution from this link : https://cmatskas.com/ef-core-migrations-with-existing-database-schema-and-data/

Respondent: SylvainB2347

Solution #3:

I’ve just had the same issue, and seems like Database.EnsureCreated() doesn’t use migrations, you can use Database.Migrate() instead which is what I’ve just tried and seems to work, but time will tell when I try another migration.

Respondent: RichardB

Solution #4:

If you’re like me and you pull a copy of your production database to work on in development, make sure all of the migrations in your Migration folder are listed in the __EFMigrationsHistory table of your database. If you move the schema to the production database after your development is complete, you’re probably not moving the migrations from that table over to your production database. If any of the migrations in your project are missing from that table, EF will try to update the database with any of the missing migrations. In my case, none of the migrations were listed in the database table, so it started by trying to add the initial migration. After adding all of the migrations to the table (except the latest one that I wanted to update the database), EF updated the database correctly.

Respondent: self_learnt

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Leave a Reply

Your email address will not be published.