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
{
}
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:
Or you do the mapping:
The take away is that you cannot have an empty OnModelCreating
Good
Bad
If you don't want to call base.OnModelCreating and want to do your own mapping, your mapping should look like this:
If you put the key for IdentityUserLogin only on UserId, you get DbEntityValidationExceptions when using the default google login.