Question

When creating a new EF6/MVC5 application using the Individual Accounts option you get a web application that has an IdentityModels.cs which contains both an IdentityUser and an IdentityDbContext. I have a separate class library where all my entities reside which has no actual link to Entity Framework because quite frankly in my opinion they shouldn't know about the technology used for storing them.

Other entities reference the ApplicationUser so logically I would now like to move my ApplicationUser class to the Entities library but because it needs to inherit from IdentityUser this would mean adding a reference to EF. This messes up my entire idea of putting them in a separate library in the first place.

Does anyone have any experience with the same problem and/or how would you handle this?

Was it helpful?

Solution

First off, entities never have a reference to EF. They're just classes. Your DbContext subclass is what stitches them into Entity Framework. Second, ApplicationUser is your user. It's not part of Entity Framework; it's merely a class that inherits from the EF model IdentityUser. If that's your problem, then you kind of just need to get over it. You can't entirely abstract away Entity Framework, unless you just stop using Entity Framework. Otherwise, there will always be a reference somewhere.

So, to just combine the two contexts so you can work with ApplicationUser within your other entities, you merely just need to change the class your application's context inherits from, from DbContext to IdentityDbContext<ApplicationUser>. Then, you can just delete the class that MVC generated for you, after of course switching the reference to your application's context in AccountsController. Again, there's no problem here inheriting from IdentityDbContext because the context is inherently tied to Entity Framework, anyways.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top