Answer the question
In order to leave comments, you need to log in
EntityFramework - is it possible to "cross" contexts?
Good afternoon.
The project has 2 contexts ( DbContext ):
- ApplicationDbContext - through which access to AspNetUser and roles is carried out
- CRMContext - for working with business logic entities, such as Appointment (meeting)
In Appointment I would like to make a field in which AspNetUser would be indicated (authorized user) - that is, an entity from another context. But both contexts work from the same database.
Can this be done?
Answer the question
In order to leave comments, you need to log in
It's very, very bad practice to use two contexts for the same database. If you need to use two contexts, then just inherit one from the other.
Can.
Contexts don't know anything about each other, and it doesn't matter if you create two instances of the same context class, or two instances of different context classes. Each instance is completely isolated and functions as if it were alone in the world. Those. you can shove any tables / entities and mappings into it, as long as they are correct from the point of view of the database schema.
The only negative is that some mappings will have to be duplicated in two places. Thus, principles like DRY and some others may be violated. But, as suggested above, inheritance closes this question.
In any case, the final decision must be made taking into account all the features of your architecture.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question