Question

I have a standard MVC 5 project in VS 2013 and using the built-in Identity provider. I am trying to customize the user profile according to http://blogs.msdn.com/b/webdev/archive/2013/10/16/customizing-profile-information-in-asp-net-identity-in-vs-2013-templates.aspx.

I did not expand on ApplicationUser as in the article as I wanted to work on the extension in another project in the same solution. I created a new sub-class ExtendedUser:

public class ExtendedUser : IdentityUser
{
    [MaxLength(12)]
    public string PIN { get; set; }
}

I then have:

public class UsersContext : DbContext
{
    public DbSet<ExtendedUser> ExtendedUsers { get; set; }

    public UsersContext() : base() { }

    public UsersContext(string ConnectionString) : base(ConnectionString) { }
}

When UsersContext is instantiated and initialized, I get hit by:

System.Data.Entity.ModelConfiguration.ModelValidationException was unhandled by user code HResult=-2146233088 Message=One or more validation errors were detected during model generation:

IdentityUserLogin: : EntityType 'IdentityUserLogin' has no key defined. Define the key for this EntityType. IdentityUserRole: : EntityType 'IdentityUserRole' has no key defined. Define the key for this EntityType. IdentityUserLogins: EntityType: EntitySet 'IdentityUserLogins' is based on type 'IdentityUserLogin' that has no keys defined. IdentityUserRoles: EntityType: EntitySet 'IdentityUserRoles' is based on type 'IdentityUserRole' that has no keys defined. ....

It seems like the other Identity membership tables are updated, but I did not touch any of those. Why is this happening and how do I correct it.

Was it helpful?

Solution

Try IdentityDbContext<ExtendedUser> as your base class for UsersContext as it is in the post you have mentioned. Without it you don't have any line of code to tell ef which property is the key. If you don't want IdentityDbContext you should in method void OnModelCreating(DbModelBuilder modelBuilder) put property mapping, something like:

   protected override void OnModelCreating(DbModelBuilder modelBuilder)
   {

       base.OnModelCreating(modelBuilder);
       modelBuilder.Entity<ExtendedUser>().HasKey(t => t.Id);

           ...

   }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top