Reputation: 4520
On my latest project, I thought it might be simpler to have only one context throughout the application. I notice that when I load a page that requires more then one query, it may return empty results.
For instance, I have a list of appointments and then a list of sales reps. They show up fine. Then I hit F5, sometimes all will keep being fine, but sometimes the appointment AND/OR rep list will be empty.
Is that a known issue with the single context apps? Is that design a bad one?
Upvotes: 3
Views: 1980
Reputation: 75296
Is that a known issue with the single context apps? Is that design a bad one?
I believe yes, with simple web application, you might not see the difference, but with complex web application with many users and required high con-currencies, the problems would be:
DbContext
implements Unit Of Work pattern under the hood, with internal cache inside, so keeping global DbContext for long time would cause the memory leak and pull tons of data from database and keep them in memory (internal cache) for the time being.
Think about Unit Of Work is a business transaction, and internal cache in Unit Of Work is just for this transaction, not for global, if the transaction is done, Unit Of Work should be disposed asap.
The best practice for DbContext in web application is keeping lifetime of DbContext as per request. If you use IoC container, most of IoC Container supports per request lifetime management.
Upvotes: 6