lundi 12 septembre 2016

How to handle one class used in multiple db contexts for one database in EF code first?

I am trying to make some module class library APIs that use EF in my repository layer. For any of that to work I need a dbcontext class in each class library. But what happens when I need one class to be referenced in each module? Take for example I have a users module whose db contexts includes:

  • Users
  • Groups
  • Roles

Then I have a locations module that includes:

  • Buildings
  • Locations
  • Rooms

Then say a third module for Equipment which has:

  • Equipment
  • Equipment Types
  • Work Orders

The latter two still need references to users, which is pretty much integral to every module. But I can't add two separate user classes to two contexts pointing to the same db, it's possible they could become out of sync then. So the obvious solution there is just have the latter 2 modules require the user module, and any class in those modules that needs the user just references the userID. This would break normalization though since it wouldn't be a foreign key so I am not sure how good that idea is.

Another possibility that popped into my head was having each module's dbcontext use an interface, and allow the person using the module to declare their own dbcontext and implement all those members, but I'm not sure that will work either.

I basically just want to make a collection of class library modules that define a common set of classes and API calls available to other programmers, while using EF as the base with the intent that this would all be stored in one DB. But I'm not quite sure how to accomplish that with how DbContexts work. What happens when you have multiple modules requiring the same object?

Aucun commentaire:

Enregistrer un commentaire