更新ASP.NET标识

时间:2017-06-16 22:47:39

标签: asp.net-mvc asp.net-identity asp.net-identity-2 ef-migrations

我有一个由ASP.NET Identity 1.0创建的ASP.NET Identity 2.0和DB项目。我想更新我的数据库。我做了以下行动:

  1. 启用迁移:
  2.   

    启用 - 迁移 - 项目名称网

         

    检查上下文是否以现有数据库为目标......

         

    为项目Web启用了代码优先迁移。

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

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

           

      脚手架迁移' UpdateIdentity'。

           

      此迁移文件的Designer代码包含您的快照   当前Code First模型。此快照用于计算   在您构建下一次迁移时更改模型。如果你   对要包含在其中的模型进行其他更改   迁移,然后您可以通过运行“添加迁移”重新构建它   UPDATEIDENTITY'试。

      但它会生成以下脚本:

      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);
      
      }
      

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

1 个答案:

答案 0 :(得分:2)

EF与之前的迁移进行了比较。如果没有,则将当前模型与空数据库进行比较。所以你需要的是创建一个初始的无代码迁移:

add-migration MyStartPoint -IgnoreChanges

现在,下一次迁移将仅是更改。因此,在您的情况下,如果您可以回滚到Identity 1,进行此迁移,更新到Identity 2,添加第二次迁移,那么您将获得所需。请参阅here