asp.net-mvc 数据库中已经有一个名为“AspNetRoles”的对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24169140/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
There is already an object named 'AspNetRoles' in the database
提问by gog
Some time ago, I created a ASP.NET MVC 5 website with the Identity 1.0 version, and i created the Identity tables with this project. Now i have to make other website using the same database for authentication, but now the Identity version is 2.0. So when i try to authenticate in the new website i get some errors.
前段时间,我用 Identity 1.0 版本创建了一个 ASP.NET MVC 5 网站,我用这个项目创建了 Identity 表。现在我必须使用相同的数据库制作其他网站进行身份验证,但现在身份版本是 2.0。因此,当我尝试在新网站中进行身份验证时,会出现一些错误。
Im trying to migrate the database using the Migrations approach, but its confused and im getting this error There is already an object named 'AspNetRoles' in the database.when i type Update-Database in the PM console.
我尝试使用 Migrations 方法迁移数据库,但是There is already an object named 'AspNetRoles' in the database.当我在 PM 控制台中键入 Update-Database 时,它感到困惑并且我收到此错误。
My question is, how is the best way to use the same database for the authetication of both sites (one using the 1.0 identity version and other using 2.0). Do I really need to migrate the database?
我的问题是,如何使用相同的数据库对两个站点进行身份验证的最佳方式(一个使用 1.0 身份版本,另一个使用 2.0)。我真的需要迁移数据库吗?
If yes, how can i solve this error that im getting?
如果是,我该如何解决我遇到的这个错误?
回答by Mohsen Esmailpour
Add-Migration InitialMigrations -IgnoreChanges
This should generate a blank "InitialMigration" file. Now, add any desired changes to the class you want. Once changes are added, run the update command again:
这应该会生成一个空白的“InitialMigration”文件。现在,向所需的类添加任何所需的更改。添加更改后,再次运行更新命令:
update-database -verbose
Now the automatic migration will be applied and the table will be altered with your changes.
现在将应用自动迁移,并且表将随着您的更改而改变。
Edit:Here is a solution to migrate identity 1 to 2 Upgrading from ASP.NET.Identity 1.0 to 2.0Use this manual migration
编辑:这是将身份 1 迁移到 2 的解决方案从 ASP.NET.Identity 1.0 升级到 2.0使用此手动迁移
public override void Up()
{
RenameColumn(table: "dbo.AspNetUserClaims", name: "User_Id", newName: "UserId");
RenameIndex(table: "dbo.AspNetUserClaims", name: "IX_User_Id", newName: "IX_UserId");
DropPrimaryKey("dbo.AspNetUserLogins");
AddColumn("dbo.AspNetUsers", "Email", c => c.String(maxLength: 256));
AddColumn("dbo.AspNetUsers", "EmailConfirmed", c => c.Boolean(nullable: false));
AddColumn("dbo.AspNetUsers", "PhoneNumber", c => c.String());
AddColumn("dbo.AspNetUsers", "PhoneNumberConfirmed", c => c.Boolean(nullable: false));
AddColumn("dbo.AspNetUsers", "TwoFactorEnabled", c => c.Boolean(nullable: false));
AddColumn("dbo.AspNetUsers", "LockoutEndDateUtc", c => c.DateTime());
AddColumn("dbo.AspNetUsers", "LockoutEnabled", c => c.Boolean(nullable: false));
AddColumn("dbo.AspNetUsers", "AccessFailedCount", c => c.Int(nullable: false));
AlterColumn("dbo.AspNetUsers", "UserName", c => c.String(nullable: false, maxLength: 256));
AlterColumn("dbo.AspNetUsers", "FirstName", c => c.String(nullable: false));
AlterColumn("dbo.AspNetUsers", "LastName", c => c.String(nullable: false));
AddColumn("dbo.AspNetUsers", "CreatedDateTime", c => c.DateTime(nullable: false));
AlterColumn("dbo.AspNetRoles", "Name", c => c.String(nullable: false, maxLength: 256));
AddPrimaryKey("dbo.AspNetUserLogins", new[] { "LoginProvider", "ProviderKey", "UserId" });
CreateIndex("dbo.AspNetUsers", "UserName", unique: true, name: "UserNameIndex");
CreateIndex("dbo.AspNetRoles", "Name", unique: true, name: "RoleNameIndex");
DropColumn("dbo.AspNetUsers", "Discriminator");
}
回答by Chris Pratt
While you can (since EF6) use migrations in two separate projects for the same database, there can't be any overlap. The way migrations work is through a dbo._MigrationHistorytable that stores the context that generated the migration and the model state of your application, which includes the Identity models.
虽然您可以(自 EF6 起)在同一个数据库的两个单独项目中使用迁移,但不能有任何重叠。迁移的工作方式是通过一个dbo._MigrationHistory表来存储生成迁移的上下文和应用程序的模型状态,其中包括身份模型。
When you try to connect your second application, it finds no previous migrations, and thus needs to generate it's initial migration, which will include tables for the Identity models, which are in its context as well. That's where your problem is.
当您尝试连接第二个应用程序时,它找不到以前的迁移,因此需要生成它的初始迁移,其中将包括身份模型的表,这些表也在其上下文中。那就是你的问题所在。
For the purposes of Identity, you need to choose one project to make the master. This one will utilize a standard IdentityDbContextwhere the Identity models will be migrated.
出于身份的目的,您需要选择一个项目来制作母版。这将使用IdentityDbContext迁移身份模型的标准。
The other project will need to be made a slave, at least in terms of utilizing Identity. So, you will need to interact with at least two contexts in this application. One will be a subclass of IdentityDbContext, but treated as database-first:
另一个项目需要成为奴隶,至少在利用身份方面。因此,您将需要与此应用程序中的至少两个上下文进行交互。一个将是 的子类IdentityDbContext,但被视为数据库优先:
public class MyIdentityContext : IdentityDbContext<ApplicationUser>
{
public MyIdentityContext()
: base("ConnectionStringNameForYourSharedDB")
{
Database.SetInitializer<MyIdentityContext>(null);
}
}
The other context will be just a regular DbContextsubclass that will be migrated as normal. You'll need to repeat this for any other project that may need access to the same Identity information from the same database. Also, because of the repetitious code this will lead to (and the fact that your ApplicationUserclass will need to be shared) you should move this code into a class library that each project can reference.
另一个上下文将只是一个正常的DbContext子类,将被正常迁移。您需要对可能需要从同一数据库访问相同身份信息的任何其他项目重复此操作。此外,由于代码重复,这将导致(以及您的ApplicationUser类需要共享的事实)您应该将此代码移动到每个项目都可以引用的类库中。
回答by Sinha Islam Owisy
In the "appsettings.json" file change the name of database
在“appsettings.json”文件中更改数据库名称
"ConnectionStrings": {
"DefaultConnection": "Server=DESKTOP-S0S1I;Database=New_name_here;Trusted_Connection=True;"
and then
进而
add-migration
update-database
It works.
有用。
回答by Jimmy
For me, the problem couldn't be fixed by adding an empty migration. My problem was that the __EFMigrationsHistory table had been manually cleared. As a consequence, the context.Database.Migrate()method thought it had to apply all the missing migrations (missing entries in __EFMigrationsHistory). Simply add all the missing migrations in the db table and it should work again.
对我来说,问题无法通过添加空迁移来解决。我的问题是 __EFMigrationsHistory 表已被手动清除。因此,该context.Database.Migrate()方法认为它必须应用所有缺失的迁移(__EFMigrationsHistory 中的缺失条目)。只需在 db 表中添加所有丢失的迁移,它应该会再次工作。
回答by pamal Sahan
for that first we have to remove migration that we newly migrated dotnet ef migrations remove -s ..\InvoiceManagement\
首先,我们必须删除我们新迁移的迁移 dotnet ef migrations remove -s ..\InvoiceManagement\
then delete *.Designer.cs file in the migration folder
然后删除迁移文件夹中的 *.Designer.cs 文件
finally dotnet ef database update -s ..\InvoiceManagement\ -s mean that startup project(webApi) remember that this code shoud be use in DBcontext file contain folder path. this code use in vscode for webapi.
最后 dotnet ef database update -s ..\InvoiceManagement\ -s 意味着启动项目(webApi)记住这个代码应该在包含文件夹路径的 DBcontext 文件中使用。此代码在 vscode 中用于 webapi。
回答by Mai Nguyen
I faced the same bug as below. Then I fixed it as below: (.NET Core / EF Core)
我遇到了与下面相同的错误。然后我将其修复如下:(.NET Core / EF Core)
Check current databases in your project:
dotnet ef migrations listIf the newest is what you've added, then remove it:
dotnet ef migrations removeGuarantee outputs of this database must be deteled in source code:
.cs/.Designer.cs files
Now it is fine. Try to re-add:
dotnet ef migrations add [new_dbo_name]Finally, try to update again, in arrangement base on migration list:
dotnet ef database update [First] dotnet ef database update [Second] ... dotnet ef database update [new_dbo_name]
检查项目中的当前数据库:
dotnet ef migrations list如果您添加的是最新的,则将其删除:
dotnet ef migrations remove必须在源代码中删除此数据库的保证输出:
.cs/.Designer.cs 文件
现在好了。尝试重新添加:
dotnet ef migrations add [new_dbo_name]最后,尝试再次更新,根据迁移列表进行排列:
dotnet ef database update [First] dotnet ef database update [Second] ... dotnet ef database update [new_dbo_name]
Hope it is helpful for you.
希望对你有帮助。

