0

for now I have this context

namespace Dafoor_MVC.Models
{
  public class DafoorDBContext : DbContext
  {
    public DbSet<Department> departments { get; set; }
    public DbSet<Course> courses { get; set; }
    public DbSet<Reply> replies { get; set; }
  }
}

This context will grow large because I have about 40 models that I wanna add.

1- is it a good idea to have the 40 models in one context ?

2- i want this context to be shared among all users, because i don't want to hit the database with queries every time if the record is already in a context, but this will affect the server memory,so how can i implement something like " last object used to be disposed or the object that didn't get called for an amount of time to be disposed from the context " ? i don't want to dispose the whole context.

3-if point 2 didn't work, can i put an instance of the context in a user session so the context will be a user specific not application spicific

Cœur
  • 37,241
  • 25
  • 195
  • 267

2 Answers2

1

is it a good idea to have the 40 models in one context?

There is nothing wrong with that if they all logically belong together.

i want this context to be shared among all users

No, you don't. You want to instantiate a context for each individual HTTP request, and dispose of it before the handling of the HTTP request is done. Do not cache your DbContext.

can i put an instance of the context in a user session so the context will be a user specific not application spicific

You should not cache your context. However, you can store objects retrieved using the context in Session. You will not be able to update the objects / object graph without first re-attaching the objects to a new context.

UPDATE

Here's why the DbContext should never be stored beyond the current HTTP request:

One DbContext per web request... why?

Community
  • 1
  • 1
Eric J.
  • 147,927
  • 63
  • 340
  • 553
  • the problem is that i wanna utilize the server memory instead of hitting the Database every time. i want the advantage of the 2nd call of find method. for ex: context.departments.find(someID) the 1st time it will use a select query but the 2nd time it will return the object from the context. – Moayad Al-sowayegh Mar 26 '13 at 18:26
  • Then you will need to store the materialized objects in Session (or in Cache, if the objects are shared across all users). Very Bad Things happen if you try to cache a DbContext, and especially if you try to share it across users. – Eric J. Mar 26 '13 at 18:34
0

1) there is no reason to worry about having 40+ DbSets in a single context class. They're simply collections and are only populated with objects you're currently using

2) instance members of DbContext and DbSet are not guaranteed to be thread safe. I don't recommend the singleton approach

3) you could, but be sure to handle database concurrency exceptions properly

Moho
  • 15,457
  • 1
  • 30
  • 31
  • No, do not store the DbContext in user session (point 3). See http://stackoverflow.com/questions/10585478/one-dbcontext-per-web-request-why – Eric J. Mar 26 '13 at 18:35