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

前端之家收集整理的这篇文章主要介绍了entity-framework – 如何将数据库模式从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相同,ef6描述了数据库的外观.另一个文件是包含用于迁移数据库的Up和Down命令的实际迁移.

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

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

一般来说,AspNetUsers和AspNetRole上的主键以及theese表的外键已经改变了长度,从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",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","UserId",principalColumn: "Id");
        migrationBuilder.AddForeignKey("FK_dbo.User_dbo.AspNetUsers_IdentityUser_Id","User","IdentityUser_Id",principalColumn: "Id");
        migrationBuilder.AddForeignKey("FK_dbo.AspNetUserLogins_dbo.AspNetUsers_UserId","AspNetUserLogins",principalColumn: "Id");
        migrationBuilder.AddForeignKey("FK_dbo.AspNetUserClaims_dbo.AspNetUsers_UserId","AspNetUserClaims",principalColumn: "Id");

        migrationBuilder.AddColumn<string>(name: "ConcurrencyStamp",table: "AspNetRoles",nullable: true);
        migrationBuilder.AddColumn<string>(name: "NormalizedName",nullable: true);

        migrationBuilder.sql(@"UPDATE AspNetRoles SET NormalizedName = UPPER(Name)");

        migrationBuilder.AddColumn<string>(name: "ConcurrencyStamp",table: "AspNetUsers",nullable: true);
        migrationBuilder.AddColumn<string>(name: "LockoutEnd",nullable: true);
        migrationBuilder.AddColumn<string>(name: "NormalizedEmail",nullable: true);
        migrationBuilder.AddColumn<string>(name: "NormalizedUserName",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",column: "NormalizedName");

        migrationBuilder.CreateIndex(
            name: "EmailIndex",column: "NormalizedEmail");

        migrationBuilder.DropIndex(
            name: "UserNameIndex",table: "AspNetUsers");
        migrationBuilder.CreateIndex(
            name: "UserNameIndex",column: "NormalizedUserName");
    }

    protected override void Down(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.DropForeignKey("FK_dbo.AspNetUserRoles_dbo.AspNetRoles_RoleId","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",principalColumn: "Id");

        migrationBuilder.DropForeignKey("FK_dbo.AspNetUserClaims_dbo.AspNetUsers_UserId","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",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",column: "Name");

        migrationBuilder.DropIndex(
            name: "EmailIndex",table: "AspNetUsers");

        migrationBuilder.DropIndex(
            name: "UserNameIndex",column: "UserName");
}

猜你在找的MsSQL相关文章