Migrate to Asp.Net Identity 2.0: New Columns Not Created in AspNetUsers Table

I get the following error when trying 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.) 

I have a small application that I just upgraded from MVC4 / Identity 1.0 to MVC5 / Identity 2.0, so I had Identity 1.0 columns (UserName, PasswordHash, SecurityStamp, Disccriminator).

  • I am using a remote hosted SQL2012 database with standard Identity 1.0 tables.
  • I created a "clean" project, registered the user, and it worked fine on localDB.
  • I successfully executed 'add-migration initial' and 'update-database' in my remote database.
  • I originally followed this official guide , and the code in step 5 was not generated. I tried to insert it manually and ran "update-database -verbose" again. It seemed like it completed successfully, but still get an error.

Appreciate any help!

migration and configuration.cs files

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

}

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

 public partial class V023 : DbMigration { public override void Up() { } public override void Down() { } } 
+8
source share
7 answers

I had the same problem. I used this manual migration, and so far I have not encountered any additional problems.

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

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

+12
source

The reason is that you upgraded to Microsoft.AspNet.Identity.EntityFramework 2.0.0.0 and changed the user context.

To fix either generate a new db (install a new mdf file of the connection string file) or modify the sql table.

+4
source

I had the same problem. Based on what Dan Gershoni said, I simply uninstalled Microsoft.AspNet.Identity.EntityFramework 2.0.0.0 and installed an older version like Microsoft.AspNet.Identity.EntityFramework -Version 1.0.0. This solved my problem.

+3
source

I tried the solution suggested by user 1502551, but it gave me problems and did not enable the Down () method. The problems I ran into were that the indexes that were changed did not exist by default from Identity 1.0, and there were a few extra columns that did not expect 2.0 / 2.1 (namely, the First and Last Name fields). Full Up () and Down () here:

 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"); } 
+1
source

You must change all entities that were updated in version 2.0, for example:

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

You also need to change the mapping of objects that have been changed in the OnModelCreating method of your dbcontext, after which you can add your migration

+1
source

Are you sure you have the correct User class in your Dbcontext? If you want to change your User table, you should inherit like this:

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

and don’t put anything like this:

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

since this will already happen in the base class.

0
source

I came across the same error. All I did was manually add the LockoutEndDateUtc column to AspNetUser

0
source

All Articles