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

瘦欲@ 提交于 2019-12-10 18:43:15

问题


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


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




回答2:


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.



来源:https://stackoverflow.com/questions/39609551/there-is-already-an-object-named-aspnetroles-in-the-database-entity-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!