0

What is the advantage of instantiating DbContext internally each time I call a repository in the same request?

Should I do this:

public class HomeController : Controller
{

    public ActionResult Index()
    {
        var repo1 = new MyRepo1();
        var repo2 = new MyRepo2();
        var repo3 = new MyRepo3();

        return View();
    }
}

Or this:

public class HomeController : Controller
{
    MyContext db = new MyContext();

    public ActionResult Index()
    {
        var repo1 = new MyRepo1(db);
        var repo2 = new MyRepo2(db);
        var repo3 = new MyRepo3(db);

        return View();
    }
}
David
  • 15,894
  • 22
  • 55
  • 66
WPalombini
  • 691
  • 9
  • 25
  • possible duplicate of [One DbContext per web request... why?](http://stackoverflow.com/questions/10585478/one-dbcontext-per-web-request-why) – Patrick Aug 15 '14 at 07:40
  • I'm voting to close since the answer in the other question discusses the pro and cons of one dbcontext per application, and one per instance of work, which is what you are asking here as well. – Patrick Aug 15 '14 at 07:42
  • I don't think that this is a duplicate question. They are very similar, but the accepted answer for that question, does not answer my. That answer starts talking about a single db context for the whole application, which is not what I am asking here. – WPalombini Aug 18 '14 at 00:36

0 Answers0