如何将数据库模式从Identity 2.2.0迁移到3.0.0-rc1-final

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何将数据库模式从Identity 2.2.0迁移到3.0.0-rc1-final相关的知识,希望对你有一定的参考价值。

我一直在玩着迁移运行在MVC5上的现有应用程序,ASP.NET Identity 2.2.0无法找到适当的信息,我想问你应该如何进行这种迁移?

我已经用了几个小时来解决这个问题,并找到了一个解决方案,允许我在旧的但迁移的数据库上使用ef7登录Identity 3.0.0-rc1-final。

我将发布我所做的答案,但我非常喜欢灵感或其他方式来完成它。

答案

首先,我使用以下命令生成了新的迁移

dnx ef migration add MigrateIdentityFrameworkFrom2to3

这在迁移文件夹中生成了两个文件

  • XYZ_MigrateIdentityFrameworkFrom2to3.cs
  • ApplicationDbContextModelSnapshot.cs

快照与ef6相同,后者描述了数据库的外观。另一个文件是包含用于迁移数据库的Up和Down命令的实际迁移。

迁移到Identity 3.0.0-rc1-final模式的问题似乎是

  • AspNetRoles两个新的数据库列(ConcurrencyStamp,NormalizedName)
  • AspNetUsers 4个新列(ConcurrencyStamp,LockoutEnd,NormalizedEmail,NormalizedUserName)
  • 新表(AspNetRoleClaims)

一般来说,AspNetUsers和AspNetRole上的主键以及这些表的外键长度已经从128改为450

以下是我以前能够登录MVC6应用程序的Up和Down命令:

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_

以上是关于如何将数据库模式从Identity 2.2.0迁移到3.0.0-rc1-final的主要内容,如果未能解决你的问题,请参考以下文章

如何将 oracle 中的 IDENTITY 列重置为新值

将现有 Microsoft.AspNet.Identity DB (EF 6) 迁移到 Microsoft.AspNetCore.Identity (EF Core)

如何在 vertica 中获取最后发布的序列 ID?

如何将 AES 模式从一种迁移到另一种?

如何使用具有最小 API .Net 6 的 Asp.net core Identity 运行迁移(第一次迁移)

如何使用 Identity ASP.NET Core 通过代码优先迁移为用户和角色播种