数据库中已经有一个名为AspNetRoles的对象. (实体框架核心) [英] There is already an object named AspNetRoles in the database. (entity-framework-core)

查看:82
本文介绍了数据库中已经有一个名为AspNetRoles的对象. (实体框架核心)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用实体框架核心的asp.net核心mvc网站.

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

我不知道它最初是如何发生的,但是我无法克服该错误: 数据库中已经有一个名为AspNetRoles的对象"

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"

我最终删除了数据库,删除了所有迁移.cs文件并从头开始.

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

然后我尝试了 add-migration MyInitialMigration

相同错误

然后我再次删除了数据库,并尝试了直接 update-database (不添加迁移)

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

仍然是相同的错误

我尝试更改appsettings配置字符串以指向新的不存在的数据库.

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

还是同样的错误!

以前,在MVC5/EF6上我遇到此错误,并使用与此答案相同的方法解决了该错误: 数据库中已经有一个命名对象

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

但是-EFCore不支持-IgnoreChanges

However -IgnoreChanges is unsupported in EFCore

或者以前在EF6上,我已经通过添加迁移来解决它,然后删除Up/Down中的所有内容并运行一个空迁移.这不适用于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

推荐答案

发现了我的问题.

我已经按照本教程从EntityFramework加载应用程序设置: https://docs.asp.net/en/latest/fundamentals/configuration.html#example-entity-framework-settings

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

在EntityFrameworkConfigurationProvider中,有一个public override void Load(),其中有一个dbContext.Database.EnsureCreated();和一个CreateAndSaveDefaultValues

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

这篇关于数据库中已经有一个名为AspNetRoles的对象. (实体框架核心)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆