M
M
mindgrow2018-02-18 18:36:56
ASP.NET
mindgrow, 2018-02-18 18:36:56

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

2 answer(s)
F
FreeBa, 2018-02-19
@mindgrow

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.

B
basrach, 2018-02-20
@basrach

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 question

Ask a Question

731 491 924 answers to any question