3

I've been trying to implement a new MVC3 project with Entity Framework 4.1, which instantiates the dbContext on Application_BeginRequest, and disposes it on Application_EndRequest

 protected virtual void Application_BeginRequest()
    {
        HttpContext.Current.Items["_EntityContext"] = new EntityContext();
    }

    protected virtual void Application_EndRequest()
    {
        var entityContext = HttpContext.Current.Items["_EntityContext"] as EntityContext;
        if (entityContext != null)
            entityContext.Dispose();
    }

The EntityContext class is defined as follows:

 public class EntityContext : MyEntities, IDisposable
{
    **//should this be static?**
    public static EntityContext Current
    {
        get { return HttpContext.Current.Items["_EntityContext"] as EntityContext; }
    }



    void IDisposable.Dispose()
    {
        Current.Dispose();
    }

My question is, will defining my Current property as static cause any problems in a multi-user scenario?

jazza1000
  • 3,396
  • 3
  • 39
  • 59
  • Related http://stackoverflow.com/questions/6987908/what-is-the-best-way-to-instantiate-and-dispose-dbcontext-in-mvc/6990244#6990244 – Eranga Apr 10 '12 at 00:28

2 Answers2

0

Your lifespan on DbContext is WAY too long. You should be spinning up a bare minimum of one per request, and even better one per access to the database.

Bryan Boettcher
  • 4,334
  • 1
  • 24
  • 49
0

As insta pointed out, you should instance the context when you actually need it. There's no advantage making your context lifespan that long.

As a side-note, there's no need to call explicitly the Dispose method, since the .NET Garbage Collector will do that more efficiently for you.

You could instance the context per class, since you're using MVC, instance the context once per Controller.

public class MyTableObjectController : Controller
{
    MyContext context = new MyContext();

    public ActionResult Index()
    {
        var model = context.MyTableObjects;

        return View(model);
    }
}

I might ask, why are you trying to keep your context available between Begin and End request? Are you trying to avoid instancing it?

Esteban
  • 3,030
  • 3
  • 29
  • 51
  • That is a good question. The problem we are trying to avoid is the dreaded "the dbcontext has been disposed" when we reference the object. I'm open to the suggestion of instantiating the context in the controller if that is a better approach. – jazza1000 Apr 09 '12 at 23:54
  • @jazza1000 I also had problems with understanding why the context was being disposed when I first started using MVC 3. Maybe the answer to [this question](http://stackoverflow.com/questions/5360372/) will help you with that. – John H Apr 10 '12 at 00:01
  • You might want to test my suggestion, you might not want to "try" it on your real project, but always you could make a new small project and test it to see how it behaves and if you feel comfortable with that approach. – Esteban Apr 10 '12 at 18:48
  • OK thanks Esteban. What I really wanted was whether someone could tell me whether there were any concurrency issues with using the static property, but I might try your method of instantiating in the controller and see if that works for us. – jazza1000 Apr 10 '12 at 22:40