更新 ASP.NET 身份

Update ASP.NET Identity

我有一个带有 ASP.NET Identity 2.0 和数据库的项目,由 ASP.NET Identity 1.0 创建。我想更新我的数据库。我执行以下操作:

  1. 启用迁移:

Enable-Migrations -ProjectName Web

Checking if the context targets an existing database...

Code First Migrations enabled for project Web.

  1. 添加迁移(我的项目有2个连接字符串,我select合适,有'old'个数据库表):

Add-Migration -Name UpdateIdentity -ProjectName Web -ConnectionStringName MainContext

Scaffolding migration 'UpdateIdentity'.

The Designer Code for this migration file includes a snapshot of your current Code First model. This snapshot is used to calculate the changes to your model when you scaffold the next migration. If you make additional changes to your model that you want to include in this migration, then you can re-scaffold it by running 'Add-Migration UpdateIdentity' again.

但它生成以下脚本:

public override void Up()
{
    CreateTable(
        "dbo.AspNetRoles",
        c => new
            {
                Id = c.String(nullable: false, maxLength: 128),
                Name = c.String(nullable: false, maxLength: 256),
            })
        .PrimaryKey(t => t.Id)
        .Index(t => t.Name, unique: true, name: "RoleNameIndex");

    CreateTable(
        "dbo.AspNetUserRoles",
        c => new
            {
                UserId = c.String(nullable: false, maxLength: 128),
                RoleId = c.String(nullable: false, maxLength: 128),
            })
        .PrimaryKey(t => new { t.UserId, t.RoleId })
        .ForeignKey("dbo.AspNetRoles", t => t.RoleId, cascadeDelete: true)
        .ForeignKey("dbo.AspNetUsers", t => t.UserId, cascadeDelete: true)
        .Index(t => t.UserId)
        .Index(t => t.RoleId);

    CreateTable(
        "dbo.AspNetUsers",
        c => new
            {
                Id = c.String(nullable: false, maxLength: 128),
                CompanyId = c.Int(nullable: false),
                SMSnumber = c.String(),
                FullName = c.String(),
                Email = c.String(maxLength: 256),
                EmailConfirmed = c.Boolean(nullable: false),
                PasswordHash = c.String(),
                SecurityStamp = c.String(),
                PhoneNumber = c.String(),
                PhoneNumberConfirmed = c.Boolean(nullable: false),
                TwoFactorEnabled = c.Boolean(nullable: false),
                LockoutEndDateUtc = c.DateTime(),
                LockoutEnabled = c.Boolean(nullable: false),
                AccessFailedCount = c.Int(nullable: false),
                UserName = c.String(nullable: false, maxLength: 256),
            })
        .PrimaryKey(t => t.Id)
        .Index(t => t.UserName, unique: true, name: "UserNameIndex");

    CreateTable(
        "dbo.AspNetUserClaims",
        c => new
            {
                Id = c.Int(nullable: false, identity: true),
                UserId = c.String(nullable: false, maxLength: 128),
                ClaimType = c.String(),
                ClaimValue = c.String(),
            })
        .PrimaryKey(t => t.Id)
        .ForeignKey("dbo.AspNetUsers", t => t.UserId, cascadeDelete: true)
        .Index(t => t.UserId);

    CreateTable(
        "dbo.AspNetUserLogins",
        c => new
            {
                LoginProvider = c.String(nullable: false, maxLength: 128),
                ProviderKey = c.String(nullable: false, maxLength: 128),
                UserId = c.String(nullable: false, maxLength: 128),
            })
        .PrimaryKey(t => new { t.LoginProvider, t.ProviderKey, t.UserId })
        .ForeignKey("dbo.AspNetUsers", t => t.UserId, cascadeDelete: true)
        .Index(t => t.UserId);

}

它试图从零开始创建表,但我需要一个脚本来修改。为什么会这样以及如何创建正确的脚本?

EF 与之前的迁移进行比较。如果有 none,它将当前模型与空数据库进行比较。因此,您需要创建一个初始的无代码迁移:

add-migration MyStartPoint -IgnoreChanges

现在下一次迁移将只进行更改。因此,在您的情况下,如果您可以回滚到身份 1,进行此迁移,更新到身份 2,添加第二次迁移,那么您将获得所需的东西。参见 here