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

15,457

Solution 1

Found my issue.

I had followed this tutorial to load appsettings from EntityFramework: https://docs.asp.net/en/latest/fundamentals/configuration.html#example-entity-framework-settings

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 :/

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/

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.

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.

Share:
15,457
mejobloggs
Author by

mejobloggs

Updated on June 05, 2022

Comments

  • mejobloggs
    mejobloggs over 1 year

    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 update-database (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

  • BHANG
    BHANG over 2 years
    I àm like you! Nice insights, thanks man! Reverted changes on my ModelSnapshot worked for me
  • Hossein
    Hossein almost 2 years
    Would you explain that how can I reach public override void Load()?
  • Hossein
    Hossein almost 2 years
    Also me and a frieand are working on a project and share the data using Git Hub, but for example when I do some changes in a table that already exists, (for example add new prop like: public int Count {get; set:} ), my friend don't get the new changes! I want to ask if your documantation is helpful in this case or not?