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

Asked

Viewed 475 times

5

I’m developing a new application on ASP.NET MVC and searching saw some examples of applications with more than one context.

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

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

I may end up having problems with relationships and data access?

There is a particular reason to use multiple contexts in a single database?

2 answers

4


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

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

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

I may end up having problems with relationships and data access?

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

There is a particular reason to use multiple contexts in a single database?

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

Another reason may be speed. Contexts with many records can impair the performance of the application. In this case, two instances of the same context are used, one reading only and the other for persistence, with different properties configurations.

0

I especially 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

  • Just as a response recommendation, try to focus your answer more on technical questions, and a little less on personal experiences, which can create an air based on opinions, which is something the site seeks to avoid. Not that personal experience cannot and should not be mentioned, but try to write a slightly larger response with a slightly denser technical basis. In experience this ensures more upvotes and the answer becomes more interesting ;)

  • 1

    Legal @Ciganomorrisonmendez , thanks for your comment without technical questions, if you want to edit it feel free to

Browser other questions tagged

You are not signed in. Login or sign up in order to post.