57

I want integrate IdentityContext with mydbcontext but i am taking this error

One or more validation errors were detected during model generation:

Ivdb.Dal.Concrete.EFCodeFirst.IdentityUserLogin: : EntityType 'IdentityUserLogin' has no key defined. Define the key for this EntityType. Ivdb.Dal.Concrete.EFCodeFirst.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.

How can i solve this ?

Code:

 public partial class ivdbDb156978Context : IdentityDbContext<ApplicationUser> 
{
    static ivdbDb156978Context()
    {
        Database.SetInitializer<ivdbDb156978Context>(null);
    }

    public ivdbDb156978Context()
        : base("Name=ivdbContext")
    {
    }


    public DbSet<Car> Cars { get; set; }

Application User

    public class ApplicationUser : IdentityUser
{

}
user1924375
  • 10,581
  • 6
  • 20
  • 27

3 Answers3

152

Your code does't show this, but from the errors you are getting I assume that you are overriding OnModelCreating.This is where IdentityDbContext<ApplicationUser> configure the entity framework mappings. This means that if you want to override OnModelCreating you need to either call the base or you must do the mapping yourself.

So either this:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);

    // your stuff here
}

Or you do the mapping:

protected override void OnModelCreating(DbModelBuilder 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 });
}
Olav Nybø
  • 11,454
  • 8
  • 42
  • 34
  • I'm sure I can find your correct answers about EF Code First approach, in the StackOverFlow whenever I need it. thanks – Vahid Ghadiri Jan 22 '14 at 07:00
  • I created an override on OnModelCreating so I could add modelBuilder.Conventions.Remove(); to prevent EF from pluralizing my table names and this bit me on the first attempt to add a migration. Thanks for your very helpful answer Olav – GDB Jan 26 '14 at 23:06
  • Hello Olav, This solved my no key defined error but now I get numerous Invalid column name errors. I've posted here http://stackoverflow.com/questions/23346422/identity-2-usermanager-find-throws-invalid-object-name-dbo-applicationuser-e. Would you have any insight on this. Thank you, Joe – Joe Apr 29 '14 at 15:58
  • base.OnModelCreating(modelBuilder); Worked for me, I have existing users so didnt want to do anything to radical. Thanks Olav – Isuru Fonseka May 08 '14 at 07:15
  • I got this error after copy pasting some of AppHarbor's documentation code. Thanks! – Rudey Jun 17 '14 at 14:14
  • base.OnModelCreating(modelBuilder); this was missing in my case – Pankaj Shrestha Jul 24 '17 at 04:31
6

If you don't want to call base.OnModelCreating and want to do your own mapping, your mapping should look like this:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<IdentityRole>().HasKey<string>(r => r.Id).Property(p => p.Name).IsRequired();
    modelBuilder.Entity<IdentityUserRole>().HasKey(r => new { r.RoleId, r.UserId });
    modelBuilder.Entity<IdentityUserLogin>().HasKey(u => new {u.UserId, u.LoginProvider, u.ProviderKey});
}

If you put the key for IdentityUserLogin only on UserId, you get DbEntityValidationExceptions when using the default google login.

AAD
  • 380
  • 3
  • 9
3

The take away is that you cannot have an empty OnModelCreating

Good

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
  base.OnModelCreating(modelBuilder);
  // your stuff here
}

Bad

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
  // base.OnModelCreating(modelBuilder);
  // your stuff here
}
Barry MSIH
  • 3,525
  • 5
  • 32
  • 53