11

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.)

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.

  • 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.

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 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()
    {

    }
}
4
  • the guide you referred to doesn't have complete migration info, multiple columns are missing from there... when you ran add-migration, did it create a migration script at all? can you share your configuration.cs and [whatever]_initial.cs?
    – dima
    Apr 3, 2014 at 1:26
  • That's good to know, thanks. I've added the mentioned files, and am looking for something that describes the full Identity 2.0 schema.
    – Jason
    Apr 3, 2014 at 21:36
  • did you try turning off automatic migrations AutomaticMigrationsEnabled = false before running add-migration and update-database?
    – dima
    Apr 3, 2014 at 22:56
  • Yes, I actually turned that to true later. I just changed it to false and ran add-migration/update-database again, same result. Appreciate the ideas.
    – Jason
    Apr 4, 2014 at 9:37

7 Answers 7

12

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

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

1
  • I think this is probably the closest answer, haven't tried it as I've since changed the overall approach (got rid of EF entirely). But it looks good, thanks.
    – Jason
    Jul 19, 2014 at 19:39
4

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

To fix either generate a new db (set a new connection string mdf file) or change the sql table.

3
  • 1
    Appreciate the response. That's a pretty good summary of the problem. If I change the table manually I don't think that solves the problem, though. Using add-migration/update-database should be changing the table, that's the point of code-first Entity Framework. I'll mark this as the answer for now, since that's the closest I've seen and right now the answer I'm trying is to ditch EF completely and implement my own data access layer by impementing my own IUserStore, odetocode.com/blogs/scott/archive/2013/11/25/…
    – Jason
    Apr 14, 2014 at 9:45
  • Related article here jskiles1.wordpress.com/2014/04/20/…
    – GibboK
    Jun 24, 2014 at 13:22
  • To add to Dan Gershony's answer: I added a blank new db to SQL, and pointed the connection strings to this new db. Ran add-migration/Update-database to build the aspNet tables for identity 2.0. I then updated my normal dev database with the new columns/names as per the resulting changes. Finally able to log in, authenticate etc.
    – callisto
    Aug 20, 2014 at 13:39
3

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

1
  • It always depends on your needs, but keep in mind that 2.0 fixes A LOT OF ISSUES. Nov 30, 2020 at 4:56
2

Tried the solution proposed by user1502551 but it gave me issues and didn't include the Down() method. The problems I was having was that the indexes being altered didn't exist by default from Identity 1.0 and there were a couple of extra columns that 2.0/2.1 doesn't expect (namely 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

You have to modify all the entities that have been 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 modify mappings of the entities that changed in the OnModelCreating method of your dbcontext, After this you can add your migration

1
  • Thanks. I've moved to Azure B2C since then so I can't test the answer, but hopefully it helps others.
    – Jason
    Nov 26, 2017 at 0:10
0

Are you sure you got the correct User class in your Dbcontext? If you want to modify your User table you should inhert like this:

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

and don t put anything like:

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

in there since it will already happen in the base class.

1
  • Appreciate your thoughts, but no, I haven't modified or added any user class at all. The only difference from the initial starter application I have is that my database has additional tables that reference the Id column from the aspnetusers table as a foreign key. I removed the FK constraint from those tables and ran migrate/update again, but it didn't make a difference.
    – Jason
    Apr 3, 2014 at 12:58
0

I ran into same error. All I did was to manually add LockoutEndDateUtc column in AspNetUser

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.