1

I have a brand-new ASP.NET MVC 5 project with one DbContext using Entity Framework 6.1 and Identity 2.2.1. Trying go use IoC, I created a 2nd DbContext and some classes belonging to that 2nd context.

Most of these classes have a foreign key to the IdentityUser class within Identity 2.2.1. The problem I'm having is that when I try to use Add-Migrations I get the following error:

One or more validation errors were detected during model generation:

{MyProjectName}.DataContexts.IdentityUserLogin: : EntityType 'IdentityUserLogin' has no key defined. Define the key for this EntityType.

{MyProjectName}.DataContexts.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.

When I move the classes I created from my 2nd context into the initial context, I don't get any error and the database table build as expected.

How can I keep IoC AND have foreign key restrictions work properly with EF and CodeFirst?

Sample

You can find a sample project at https://github.com/RoLYroLLs/MultiDbContextForeignKeySample. To recreate the error run this on the Package Manager Console:

Enable-Migrations -MigrationsDirectory "DataContexts\ClientMigrations" -ContextTypeName MultiDbContextForeignKeySample.DataContexts.ClientDbContext
Community
  • 1
  • 1
RoLYroLLs
  • 3,113
  • 4
  • 38
  • 57
  • @gert-arnold, what makes you think this is a duplicate as the one you marked it as? Did you really read the error I have having? Do you need me to expand my explanation? – RoLYroLLs Dec 13 '15 at 20:33
  • @GertArnold, No problem. Thank you for reopening it. I'm making a sample project for someone to help out easily just in case. – RoLYroLLs Dec 13 '15 at 20:39
  • If you are going to reference Identity's ApplicationUser you either need to configure your classes under a context that inherits from IdentityDbContext as you have seen, or you could just clone a user class in your app context that maps to AspNetUsers and is used for references. – Steve Greene Dec 14 '15 at 14:17
  • @SteveGreene since I'm still fairly new to EF/C# can you provide an example on the second method? If you don't mind can you use my sample project and provide a pull request. Thanks. – RoLYroLLs Dec 14 '15 at 14:20
  • @RoLYroLLs did you ever get an example of what you're trying to do? – Connor Spangler Dec 22 '15 at 19:29
  • @EschersEnigma I have not. – RoLYroLLs Dec 22 '15 at 19:30
  • Hi @EschersEnigma, I haven't received anything on this. Do you have a possible answer you can provide? Thanks! – RoLYroLLs Jun 14 '16 at 15:24
  • Nope. I actually became so frustrated attempting to do this, I completely dropped the project. It's an absolute nightmare to work with IdentityFramework unless you have someone extremely experienced sitting next to you. I suggest you look at more reasonable frameworks. – Connor Spangler Jun 14 '16 at 15:25
  • @EschersEnigma, ok thank you for trying. I understand the frustration. =) – RoLYroLLs Jun 14 '16 at 15:26

0 Answers0