迁移到 Asp.Net Identity 2.0:未在 AspNetUsers 表中创建新列 [英] Migrating to Asp.Net Identity 2.0: new columns not being created in AspNetUsers table

查看:30
本文介绍了迁移到 Asp.Net Identity 2.0:未在 AspNetUsers 表中创建新列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试使用 Identity 2.0 和默认 MVC 5 应用程序注册新用户时,出现以下错误:

I get the following error when I try to register a new user, using Identity 2.0 and the default MVC 5 application:

Invalid column name 'Email'.
Invalid column name 'EmailConfirmed'.
Invalid column name 'PhoneNumber'.
Invalid column name 'PhoneNumberConfirmed'.
Invalid column name 'TwoFactorEnabled'.
Invalid column name 'LockoutEndDateUtc'.
Invalid column name 'LockoutEnabled'.
Invalid column name 'AccessFailedCount'.
(repeats 2 more times, I have a total of 4 test users in AspNetUsers table.)

我有一个小应用程序,我刚刚从 MVC4/Identity 1.0 升级到 MVC5/Identity 2.0,所以我让 Identity 1.0 列(UserName、PasswordHash、SecurityStamp、Discriminator)正常工作.

I have a small application I've just upgraded from MVC4/Identity 1.0 to MVC5/Identity 2.0, so I had the Identity 1.0 columns (UserName, PasswordHash, SecurityStamp, Discriminator) working.

  • 我使用的是带有标准 Identity 1.0 表的远程托管 SQL2012 数据库.
  • 我创建了一个干净"的项目,注册了一个用户,它在 localDB 上运行良好.
  • 我在远程数据库上成功运行了add-migration initial"和update-database".
  • 我最初关注了 这个官方指南,并且没有生成第 5 步中的代码.我尝试手动粘贴它并再次运行update-database -verbose".似乎已成功完成,但仍然出现错误.
  • I'm was using a remotely hosted SQL2012 DB with the standard Identity 1.0 tables.
  • I created a 'clean' project, registered a user and it ran fine on the localDB.
  • I successfully ran 'add-migration initial' and 'update-database' on my remote database.
  • I initially followed this official guide , and the code in step 5 wasn't being generated. I tried pasting it in by hand and ran "update-database -verbose" again. Seemed to complete successfully, but still get the error.

感谢任何帮助!

迁移和配置.cs 文件

internal sealed class Configuration : DbMigrationsConfiguration<FactBanker.Models.ApplicationDbContext>
{
    public Configuration()
    {
        AutomaticMigrationsEnabled = true;
    }

    protected override void Seed(FactBanker.Models.ApplicationDbContext context)
    {

    }
}

}

我的 V023 migration.cs 文件Up() 和 Down() 方法都是空的.

My V023 migration.cs file Both the Up() and Down() methods were empty.

public partial class V023 : DbMigration
{
    public override void Up()
    {

    }

    public override void Down()
    {

    }
}

推荐答案

我遇到了同样的问题.我使用了此手动迁移,但尚未遇到任何其他问题.

I was having the same problem. I used this manual migration and I haven't run into any additional issues yet.

public override void Up()
{
    RenameColumn(table: "dbo.AspNetUserClaims", name: "User_Id", newName: "UserId");
    RenameIndex(table: "dbo.AspNetUserClaims", name: "IX_User_Id", newName: "IX_UserId");
    DropPrimaryKey("dbo.AspNetUserLogins");
    AddColumn("dbo.AspNetUsers", "Email", c => c.String(maxLength: 256));
    AddColumn("dbo.AspNetUsers", "EmailConfirmed", c => c.Boolean(nullable: false));
    AddColumn("dbo.AspNetUsers", "PhoneNumber", c => c.String()); 
    AddColumn("dbo.AspNetUsers", "PhoneNumberConfirmed", c => c.Boolean(nullable: false));
    AddColumn("dbo.AspNetUsers", "TwoFactorEnabled", c => c.Boolean(nullable: false));
    AddColumn("dbo.AspNetUsers", "LockoutEndDateUtc", c => c.DateTime());
    AddColumn("dbo.AspNetUsers", "LockoutEnabled", c => c.Boolean(nullable: false));
    AddColumn("dbo.AspNetUsers", "AccessFailedCount", c => c.Int(nullable: false));
    AlterColumn("dbo.AspNetUsers", "UserName", c => c.String(nullable: false, maxLength: 256));
    AlterColumn("dbo.AspNetUsers", "FirstName", c => c.String(nullable: false));
    AlterColumn("dbo.AspNetUsers", "LastName", c => c.String(nullable: false));
    AddColumn("dbo.AspNetUsers", "CreatedDateTime", c => c.DateTime(nullable: false));
    AlterColumn("dbo.AspNetRoles", "Name", c => c.String(nullable: false, maxLength: 256));
    AddPrimaryKey("dbo.AspNetUserLogins", new[] { "LoginProvider", "ProviderKey", "UserId" });
    CreateIndex("dbo.AspNetUsers", "UserName", unique: true, name: "UserNameIndex");
    CreateIndex("dbo.AspNetRoles", "Name", unique: true, name: "RoleNameIndex");
    DropColumn("dbo.AspNetUsers", "Discriminator");
} 

取自:http://adamstephensen.com/2014/05/02/upgrading-from-asp-net-identity-1-0-to-2-0/

这篇关于迁移到 Asp.Net Identity 2.0:未在 AspNetUsers 表中创建新列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆