迁移到Asp.Net Identity 2.0:未在AspNetUsers表中创建新列

时间:2014-04-02 16:23:23

标签: entity-framework asp.net-mvc-5 identity

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

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)工作。

  • 我正在使用带有标准Identity 1.0表的远程托管SQL2012数据库。
  • 我创建了一个'干净'项目,注册了一个用户,并在localDB上运行良好。
  • 我在远程数据库上成功运行了“add-migration initial”和“update-database”。
  • 我最初关注 this official guide ,并且未生成步骤5中的代码。我尝试手动粘贴它并再次运行“update-database -verbose”。似乎已成功完成,但仍然收到错误。

感谢任何帮助!

迁移和配置文件

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()方法都是空的。

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

    }

    public override void Down()
    {

    }
}

7 个答案:

答案 0 :(得分:11)

我遇到了同样的问题。我使用了这种手动迁移,但我还没有遇到任何其他问题。

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/

答案 1 :(得分:4)

原因是您已升级到Microsoft.AspNet.Identity.EntityFramework 2.0.0.0并且更改了用户上下文。

修复生成新数据库(设置新连接字符串mdf文件)或更改sql表。

答案 2 :(得分:3)

我遇到了同样的问题。根据Dan Gershony的说法,我只是卸载了Microsoft.AspNet.Identity.EntityFramework 2.0.0.0并安装了旧版本,如Microsoft.AspNet.Identity.EntityFramework -Version 1.0.0。这解决了我的问题。

答案 3 :(得分:1)

尝试了user1502551提出的解决方案,但它给了我一些问题,但没有包含Down()方法。我遇到的问题是默认情况下,Identity 1.0中不存在被修改的索引,而且还有一些2.0 / 2.1不期望的额外列(即First和Last Name字段)。 Full Up()和Down():

public override void Up()
    {
        RenameColumn(table: "dbo.AspNetUserClaims", name: "User_Id", newName: "UserId");
        AddColumn("dbo.AspNetUsers", "Email", c => c.String());
        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));
        DropColumn("dbo.AspNetUsers", "Discriminator");
    }

    public override void Down()
    {
        AddColumn("dbo.AspNetUsers", "Discriminator", c => c.String(nullable: false, maxLength: 128));
        AlterColumn("dbo.AspNetUsers", "UserName", c => c.String(nullable: true));
        DropColumn("dbo.AspNetUsers", "AccessFailedCount");
        DropColumn("dbo.AspNetUsers", "LockoutEnabled");
        DropColumn("dbo.AspNetUsers", "LockoutEndDateUtc");
        DropColumn("dbo.AspNetUsers", "TwoFactorEnabled");
        DropColumn("dbo.AspNetUsers", "PhoneNumberConfirmed");
        DropColumn("dbo.AspNetUsers", "PhoneNumber");
        DropColumn("dbo.AspNetUsers", "EmailConfirmed");
        DropColumn("dbo.AspNetUsers", "Email");
        RenameColumn(table: "dbo.AspNetUserClaims", name: "UserId", newName: "User_Id");
    }

答案 4 :(得分:1)

您必须修改2.0版中已更新的所有实体,例如:

public partial class AspNetUser
{    
    public AspNetUser()
    {
        AspNetUserClaims = new HashSet<AspNetUserClaim>();
        AspNetUserLogins = new HashSet<AspNetUserLogin>();
        AspNetRoles = new HashSet<AspNetRole>();
    }

    public string Id { get; set; }

    [StringLength(256)]
    public string UserName { get; set; }

    public string PasswordHash { get; set; }

    public string SecurityStamp { get; set; }

    [MaxLength(256)]
    public string Email { get; set; }

    public bool EmailConfirmed { get; set; }

    public string PhoneNumber { get; set; }

    public bool PhoneNumberConfirmed { get; set; }

    public bool TwoFactorEnabled { get; set; }

    public DateTime LockoutEndDateUtc { get; set; }

    public bool LockoutEnabled { get; set; }

    public int AccessFailedCount { get; set; }

    public int UserId { get; set; }

    public virtual ICollection<AspNetUserClaim> AspNetUserClaims { get; set; }

    public virtual ICollection<AspNetUserLogin> AspNetUserLogins { get; set; }

    public virtual User User { get; set; }

    public virtual ICollection<AspNetRole> AspNetRoles { get; set; }
}

public partial class AspNetUserClaim
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    public string ClaimType { get; set; }

    public string ClaimValue { get; set; }

    [Required]
    [StringLength(128)]
    public string UserId { get; set; }

    public virtual AspNetUser AspNetUser { get; set; }
}

您还需要修改dbcontext的OnModelCreating方法中更改的实体的映射,之后您可以添加迁移

答案 5 :(得分:0)

你确定你的Dbcontext中有正确的User类吗?如果你想修改你的用户表,你应该这样:

public class ApplicationDbContext : IdentityDbContext<MyCustomUserClass>
{
    ...
}

并且不要放任何类似的东西:

public DbSet<MyCustomUserClass> Users { get; set; }

因为它已经在基类中发生了。

答案 6 :(得分:0)

我遇到了同样的错误。我要做的就是在AspNetUser中手动添加LockoutEndDateUtc列