Reputation: 1193
I would like to use IdentityDbContext for my security in my mvc5 application. My application's datacontext should have relationships to my subclass of IdentityUser in IdentityDbContext. When I try to create these relationships (many-to-many: a MyIdentityUser has many Foo's and a Foo has many MyIdentityUser's), I get 'no key define' errors:
MyNamespace.IdentityUserLogin: : EntityType 'IdentityUserLogin' has no key defined. Define the key for this EntityType.
This Question seems to indicate that I am going about it the wrong way and should instead create a single context that has everything in it. My question is this: What's the correct approach to have a single database host the MVC5 security types (so I can use UserManager, Authorize attributes, etc.) along with the rest of my application data?
Should I just subclass IdentityDbContext for my 'uber-model'? Try to re-create the entities and mappings in IdentityDbContext? Something else?
Upvotes: 1
Views: 326
Reputation: 999
Create the two following classes
public class IdentityUserLoginConfiguration : EntityTypeConfiguration<IdentityUserLogin>
{
public IdentityUserLoginConfiguration()
{
HasKey(iul => iul.UserId);
}
}
public class IdentityUserRoleConfiguration : EntityTypeConfiguration<IdentityUserRole>
{
public IdentityUserRoleConfiguration()
{
HasKey(iur => iur.RoleId);
}
}
In the OnModelCreating method within your Applications DbContext add the two configurations outlined above to the model:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new IdentityUserLoginConfiguration());
modelBuilder.Configurations.Add(new IdentityUserRoleConfiguration());
}
This should now get rid of the error methods when your model is being created. It did for me.
Upvotes: 1