Collection of IdentyUsers in EntityFramework - has no key defined

asp.net-identity entity-framework entity-framework-6

Question

All works well till i decide to add IdentityUser collection in another entity as navigation property.

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.

But they are part of Microsoft.AspNet.Identity.EntityFramework library. How is it that they has no keys defined???

My entities and db context (EF v6.1, AspNet.Identity v.2.0):

public class User : IdentityUser
{
    public virtual int OrganizationId { get; set; }
    public virtual Organization Organization { get; set; }
}

public class Organization
{
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
    public virtual ICollection<User> Users { get; set; }
}

public class DatabaseModelContext : IdentityDbContext<User>
{
    public DatabaseModelContext() : base("ConnectionString") { }
    public DbSet<Organization> Organizations { get; set; }
}

Also tried to define explicit relations:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);
    modelBuilder.Entity<Organization>()
                .HasMany(o => o.Users)
                .WithOptional(u => u.Organization)
                .HasForeignKey(u => u.OrganizationId);
    modelBuilder.Entity<User>()
                .HasOptional(u => u.Organization)
                .WithMany()
                .HasForeignKey(u => u.OrganizationId);
}

NO success!!!

It cant just be, that one must define key types for entity framework internal classes!!!

SOLUTION (update)

Actually my simplified example not reflecting my real app situation. My apologies. I've been using multiple Dbcontexts in my app and organization entity come from separate context what derives form Dbcontext class. In that way this context know nothing about IdentityUser models but Organization entity involves User entity - there is a problem.

Solution - i derived all my other context from IdentityDbContext !!!

1
2
3/24/2014 10:54:08 PM

Accepted Answer

Actually my simplified example not reflecting my real app situation. My apologies. I've been using multiple Dbcontexts in my app and organization entity come from separate context what derives form Dbcontext class. In that way this context know nothing about IdentityUser models but Organization entity involves User entity - there is a problem.

Solution - i derived all my other context from IdentityDbContext !!!

5
3/24/2014 10:55:21 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow