Is it feasible to use more than one DbContext for the same database?

4

I'm developing a new application in ASP.NET MVC and searching I've seen some examples of applications with more than one context.

Let's say my application has several different modules, but all entities have some connection.

In this scenario, is it feasible to use more than one context?

Can I end up having relationship and data access issues?

Is there a special reason to use multiple contexts in a single database?

    
asked by anonymous 09.03.2015 / 21:07

2 answers

3

In this scenario, is it feasible to use more than one context?

Yes, both is feasible and recommended for some cases, where the visibility of all entities in a given context is not interesting.

For example, in ASP.NET Identity, a context separate from the rest of the application is usually used.

Can I end up having relationship and data access issues?

It can. For example, if you make two selections in different contexts and relate the entity in some way (as in the case where you want to create associative records from cardinality n to n), there may be inconsistencies because the saved context can understand the object of the other context as being a new object, which does not yet exist, even leading to duplication of data.

Is there a particular reason to use multiple contexts in a single database?

As already mentioned above, in a scenario of authentication and validation of logins is a classic scenario for this separation.

Another reason may be speed. Contexts with many records can degrade application performance. In this case, two instances of the same context are used, one read-only and one persistence, with different properties settings.

    
09.03.2015 / 21:36
0

I specifically only use more than one DbContext for different bases, summarizing create dbcontext for different bases, otherwise I see no need

In some cases I see people creating more than one dbcontext just for the login area

    
09.03.2015 / 21:25