问题描述
我一直在尝试使用 ASP.NET Identity 2.2.0 迁移在 MVC5 上运行的现有应用程序,但无法找到正确的信息,我想问一下您应该如何进行此迁移?
I've been playing around migrating an existing application running on MVC5 with ASP.NET Identity 2.2.0 having trouble finding proper information, i'd like to ask how you are supposed to do this migration?
我已经用了几个小时来解决这个问题,并找到了一个解决方案,该解决方案允许我在旧的但已迁移的数据库上使用身份 3.0.0-rc1-final 和 ef7 登录.
I've used a couple of hours on this problem and have found a solution that allows me to log in with Identity 3.0.0-rc1-final with ef7 on my old but migrated database.
我将发布我所做的作为答案,但我非常想要灵感或其他方式来完成它.
I will post what I did as an answer, but i'd very much like inspiration or other ways to have done it.
推荐答案
首先我使用以下命令生成了一个新的迁移
First I generated a new migration with the following command
dnx ef migration add MigrateIdentityFrameworkFrom2to3
这在 Migrations 文件夹中生成了两个文件
This generated two files in the folder Migrations
- XYZ_MigrateIdentityFrameworkFrom2to3.cs
- ApplicationDbContextModelSnapshot.cs
快照与 ef6 相同,是对数据库外观的描述.另一个文件是包含用于迁移数据库的 Up 和 Down 命令的实际迁移.
The snapshot is the same as with ef6, a description of how your database looks. the other file is the actual migration containing the Up and Down commands for migrating your database.
迁移到 Identity 3.0.0-rc1-final 架构的问题似乎是
The problems with migrating to the Identity 3.0.0-rc1-final schema appeared to be
- AspNetRoles 两个新的数据库列(ConcurrencyStamp、NormalizedName)
- AspNetUsers 4 个新列(ConcurrencyStamp、LockoutEnd、NormalizedEmail、NormalizedUserName)
- 新表 (AspNetRoleClaims)
一般来说,AspNetUsers 和 AspNetRole 上的主键以及这些表的外键长度已从 128 更改为 450
And in general the primary key on AspNetUsers and AspNetRole and Foreign Keys to theese tables had changed in length, from 128 to 450
以下是我用来登录 MVC6 应用程序的 Up 和 Down 命令:
The following is the Up and Down commands I used to be able to log in to my MVC6 application:
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropForeignKey("FK_dbo.AspNetUserRoles_dbo.AspNetRoles_RoleId", "AspNetUserRoles");
migrationBuilder.DropPrimaryKey("PK_dbo.AspNetRoles", "AspNetRoles");
migrationBuilder.Sql(@"
ALTER TABLE[AspNetRoles]
ALTER COLUMN[Id] NVARCHAR(450) NOT NULL
ALTER TABLE[AspNetUserRoles]
ALTER COLUMN[RoleId] NVARCHAR(450) NOT NULL");
migrationBuilder.AddPrimaryKey("PK_dbo.AspNetRoles", "AspNetRoles", "Id");
migrationBuilder.AddForeignKey("FK_dbo.AspNetUserRoles_dbo.AspNetRoles_RoleId", "AspNetUserRoles", "RoleId", "AspNetRoles", principalColumn:"Id");
migrationBuilder.DropForeignKey("FK_dbo.AspNetUserClaims_dbo.AspNetUsers_UserId", "AspNetUserClaims");
migrationBuilder.DropForeignKey("FK_dbo.AspNetUserLogins_dbo.AspNetUsers_UserId", "AspNetUserLogins");
migrationBuilder.DropForeignKey("FK_dbo.User_dbo.AspNetUsers_IdentityUser_Id", "User");
migrationBuilder.DropForeignKey("FK_dbo.AspNetUserRoles_dbo.AspNetUsers_UserId", "AspNetUserRoles");
migrationBuilder.DropPrimaryKey("PK_dbo.AspNetUsers", "AspNetUsers");
migrationBuilder.Sql(@"
ALTER TABLE [AspNetUsers]
ALTER COLUMN [Id] NVARCHAR(450) NOT NULL
ALTER TABLE[AspNetUserRoles]
ALTER COLUMN[UserId] NVARCHAR(450) NOT NULL
ALTER TABLE[User]
ALTER COLUMN[IdentityUser_Id] NVARCHAR(450) NOT NULL
ALTER TABLE[AspNetUserLogins]
ALTER COLUMN[UserId] NVARCHAR(450) NOT NULL
ALTER TABLE[AspNetUserClaims]
ALTER COLUMN[UserId] NVARCHAR(450) NOT NULL");
migrationBuilder.AddPrimaryKey("PK_dbo.AspNetUsers", "AspNetUsers", "Id");
migrationBuilder.AddForeignKey("FK_dbo.AspNetUserRoles_dbo.AspNetUsers_UserId", "AspNetUserRoles", "UserId", "AspNetUsers", principalColumn: "Id");
migrationBuilder.AddForeignKey("FK_dbo.User_dbo.AspNetUsers_IdentityUser_Id", "User", "IdentityUser_Id", "AspNetUsers", principalColumn: "Id");
migrationBuilder.AddForeignKey("FK_dbo.AspNetUserLogins_dbo.AspNetUsers_UserId", "AspNetUserLogins", "UserId", "AspNetUsers", principalColumn: "Id");
migrationBuilder.AddForeignKey("FK_dbo.AspNetUserClaims_dbo.AspNetUsers_UserId", "AspNetUserClaims", "UserId", "AspNetUsers", principalColumn: "Id");
migrationBuilder.AddColumn<string>(name: "ConcurrencyStamp", table: "AspNetRoles", nullable: true);
migrationBuilder.AddColumn<string>(name: "NormalizedName", table: "AspNetRoles", nullable: true);
migrationBuilder.Sql(@"UPDATE AspNetRoles SET NormalizedName = UPPER(Name)");
migrationBuilder.AddColumn<string>(name: "ConcurrencyStamp", table: "AspNetUsers", nullable: true);
migrationBuilder.AddColumn<string>(name: "LockoutEnd", table: "AspNetUsers", nullable: true);
migrationBuilder.AddColumn<string>(name: "NormalizedEmail", table: "AspNetUsers", nullable: true);
migrationBuilder.AddColumn<string>(name: "NormalizedUserName", table: "AspNetUsers", nullable: true);
migrationBuilder.Sql(@"UPDATE AspNetUsers SET NormalizedEmail = UPPER(Email), NormalizedUserName = UPPER(UserName)"); // MVC6 utilizes Email as login by default with forms authentication, and searches for the email in NormalizedUserName, I changed the login formular to utilize UserName instead of email when logging in, alternatively you can put in the email as NormalizedUserName.
migrationBuilder.CreateTable(
name: "AspNetRoleClaims",
columns: table => new
{
Id = table.Column<int>(nullable: false)
.Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
ClaimType = table.Column<string>(nullable: true),
ClaimValue = table.Column<string>(nullable: true),
RoleId = table.Column<string>(nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_IdentityRoleClaim<string>", x => x.Id);
table.ForeignKey(
name: "FK_IdentityRoleClaim<string>_IdentityRole_RoleId",
column: x => x.RoleId,
principalTable: "AspNetRoles",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.AddColumn<string>(name: "ProviderDisplayName", table: "AspNetUserLogins", nullable: true);
migrationBuilder.DropIndex(
name: "RoleNameIndex",
table: "AspNetRoles");
migrationBuilder.CreateIndex(
name: "RoleNameIndex",
table: "AspNetRoles",
column: "NormalizedName");
migrationBuilder.CreateIndex(
name: "EmailIndex",
table: "AspNetUsers",
column: "NormalizedEmail");
migrationBuilder.DropIndex(
name: "UserNameIndex",
table: "AspNetUsers");
migrationBuilder.CreateIndex(
name: "UserNameIndex",
table: "AspNetUsers",
column: "NormalizedUserName");
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropForeignKey("FK_dbo.AspNetUserRoles_dbo.AspNetRoles_RoleId", "AspNetUserRoles");
migrationBuilder.DropPrimaryKey("PK_dbo.AspNetRoles", "AspNetRoles");
migrationBuilder.Sql(@"ALTER TABLE [AspNetRoles]
ALTER COLUMN [Id] NVARCHAR(128) NOT NULL
ALTER TABLE[AspNetUserRoles]
ALTER COLUMN[RoleId] NVARCHAR(128) NOT NULL");
migrationBuilder.AddPrimaryKey("PK_dbo.AspNetRoles", "AspNetRoles", "Id");
migrationBuilder.AddForeignKey("FK_dbo.AspNetUserRoles_dbo.AspNetRoles_RoleId", "AspNetUserRoles", "RoleId", "AspNetRoles", principalColumn: "Id");
migrationBuilder.DropForeignKey("FK_dbo.AspNetUserClaims_dbo.AspNetUsers_UserId", "AspNetUserClaims");
migrationBuilder.DropForeignKey("FK_dbo.AspNetUserLogins_dbo.AspNetUsers_UserId", "AspNetUserLogins");
migrationBuilder.DropForeignKey("FK_dbo.User_dbo.AspNetUsers_IdentityUser_Id", "User");
migrationBuilder.DropForeignKey("FK_dbo.AspNetUserRoles_dbo.AspNetUsers_UserId", "AspNetUserRoles");
migrationBuilder.DropPrimaryKey("PK_dbo.AspNetUsers", "AspNetUsers");
migrationBuilder.Sql(@"ALTER TABLE [AspNetUsers]
ALTER COLUMN [Id] NVARCHAR(128) NOT NULL
ALTER TABLE[AspNetUserRoles]
ALTER COLUMN[UserId] NVARCHAR(128) NOT NULL
ALTER TABLE[User]
ALTER COLUMN[IdentityUser_Id] NVARCHAR(128) NOT NULL
ALTER TABLE[AspNetUserLogins]
ALTER COLUMN[UserId] NVARCHAR(128) NOT NULL
ALTER TABLE[AspNetUserClaims]
ALTER COLUMN[UserId] NVARCHAR(128) NOT NULL");
migrationBuilder.AddPrimaryKey("PK_dbo.AspNetUsers", "AspNetUsers", "Id");
migrationBuilder.AddForeignKey("FK_dbo.AspNetUserRoles_dbo.AspNetUsers_UserId", "AspNetUserRoles", "UserId", "AspNetUsers", principalColumn: "Id");
migrationBuilder.AddForeignKey("FK_dbo.User_dbo.AspNetUsers_IdentityUser_Id", "User", "IdentityUser_Id", "AspNetUsers", principalColumn: "Id");
migrationBuilder.AddForeignKey("FK_dbo.AspNetUserLogins_dbo.AspNetUsers_UserId", "AspNetUserLogins", "UserId", "AspNetUsers", principalColumn: "Id");
migrationBuilder.AddForeignKey("FK_dbo.AspNetUserClaims_dbo.AspNetUsers_UserId", "AspNetUserClaims", "UserId", "AspNetUsers", principalColumn: "Id");
migrationBuilder.DropTable("AspNetRoleClaims");
migrationBuilder.DropColumn(name: "ConcurrencyStamp", table: "AspNetRoles");
migrationBuilder.DropColumn(name: "NormalizedName", table: "AspNetRoles");
migrationBuilder.DropColumn(name: "ConcurrencyStamp", table: "AspNetUsers");
migrationBuilder.DropColumn(name: "LockoutEnd", table: "AspNetUsers");
migrationBuilder.DropColumn(name: "NormalizedEmail", table: "AspNetUsers");
migrationBuilder.DropColumn(name: "NormalizedUserName", table: "AspNetUsers");
migrationBuilder.DropColumn(name: "ProviderDisplayName", table: "AspNetUserLogins");
migrationBuilder.DropIndex(
name: "RoleNameIndex",
table: "AspNetRoles");
migrationBuilder.CreateIndex(
name: "RoleNameIndex",
table: "AspNetRoles",
column: "Name");
migrationBuilder.DropIndex(
name: "EmailIndex",
table: "AspNetUsers");
migrationBuilder.DropIndex(
name: "UserNameIndex",
table: "AspNetUsers");
migrationBuilder.CreateIndex(
name: "UserNameIndex",
table: "AspNetUsers",
column: "UserName");
}
这篇关于如何将数据库架构从 Identity 2.2.0 迁移到 3.0.0-rc1-final的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!