多个DbContext与单个DbContext - 数据库优先

时间:2014-08-15 05:58:54

标签: c# asp.net-mvc entity-framework

每次在同一个请求中调用存储库时,在内部实例化DbContext有什么好处?

我应该这样做:

public class HomeController : Controller
{

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

        return View();
    }
}

或者这个:

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();
    }
}

0 个答案:

没有答案