13

I am trying to add profile/Membership information into my MVC5 application and adding configuration mappings.

I get the following error message:

my.Models.IdentityUserLogin: : EntityType 'IdentityUserLogin' has no key defined. Define the key for this EntityType.

my.Models.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.

public class ApplicationUser : IdentityUser
{
    public string City { get; set; }
    public string Discriminator { get; set; }

    public string Address { get; set; }     
}

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("DefaultConnection")
    {
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Configurations.Add(new ApplicationUserConfiguration());           
    }
}
Amirhossein Mehrvarzi
  • 18,024
  • 7
  • 45
  • 70
Alex51482
  • 133
  • 1
  • 4

3 Answers3

22

Calling base.OnModelCreating(modelBuilder) did not solve the issue for me.

The behavior of Microsoft.AspNet.Identity.EntityFramework seems to be different in VS2013-Preview, VS2013-RC, and VS2013-RTM. I'm using the RTM version.

After inheriting from IdentityUser I had to recreate all other primary keys in the model to make it work:

public class ApplicationUser : IdentityUser
{
    public string DisplayName { get; set; }
}


public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext() : base("DefaultConnection") { }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        modelBuilder.Entity<IdentityUserLogin>().HasKey<string>(l => l.UserId);
        modelBuilder.Entity<IdentityRole>().HasKey<string>(r => r.Id);
        modelBuilder.Entity<IdentityUserRole>().HasKey(r => new { r.RoleId, r.UserId });
    }

(See Configuring/Mapping Properties and Types with the Fluent API)

I guess work on AspNet.Identity.EntityFramework is ongoing and this will be fixed (?)

flip
  • 1,271
  • 14
  • 28
  • I've had to do exactly the same thing to get by this issue. I thought that EF would pick out the entity IDs automatically? – A. Murray Nov 06 '13 at 12:04
  • 2
    This resolved it all for me! Many thanks - should be marked as answered :) – Darren Wainwright Jan 15 '14 at 19:00
  • I have had a similar problem and tried this solution and thought it fixed the problem but I am getting few extra tables in the database such as IdentityUsers, and I also still have AspNetUsers table. What did i do wrong? – Laurence Feb 17 '14 at 09:33
  • @LaurenceNyein place base.OnModelCreating(modelBuilder) as last call in methode. – Arvis Mar 22 '14 at 16:04
  • http://stackoverflow.com/questions/19913447/user-in-entity-type-mvc5-ef6 fixed it for me – Justin Skiles Apr 21 '14 at 04:02
19

Call base.OnModelCreating(modelBuilder) after configuration added.

jd4u
  • 5,789
  • 2
  • 28
  • 28
  • 2
    if this doesnt work..try calling it before the configurationaded – taher chhabrawala Oct 20 '13 at 14:21
  • 1
    It would seem `base.OnModelCreating(modelBuilder);` must be called if `IdentityDbContext` is used, `OnModelCreating` overridden, and I am not manually configuring `IdentityUserLogin`, `IdentityUserRole` and `IdentityUserClaim` with attributes or the fluent API. – Jeremy Cook Nov 25 '13 at 14:07
0

Follow these steps around OnModelCreating method and test after each one to be aware of taking effect:

  1. Make sure you have one Context to prevent of their rule conflicts.
  2. Call base.OnModelCreating(modelBuilder); inside the mentioned method (first of all)
  3. Add the followings plus previews step in the method:


modelBuilder.Entity<IdentityUserLogin>().HasKey<string>(l => l.UserId);
modelBuilder.Entity<IdentityRole>().HasKey<string>(r => r.Id);
modelBuilder.Entity<IdentityUserRole>().HasKey(r => new { r.RoleId, r.UserId });
Amirhossein Mehrvarzi
  • 18,024
  • 7
  • 45
  • 70