Declaring Entity FrameWork Contexts with using

前端 未结 3 1995
广开言路
广开言路 2021-02-08 09:40

Which is the Best Practise in Declaring Entity FrameWork Contexts

function()
{
    DBContext context = new DBContext();

    //Entity code

    return ;
}
         


        
3条回答
  •  失恋的感觉
    2021-02-08 10:12

    Your request will be executed toward the datasource as soon as you'll call .ToList() method.

    That's why you cannot perform .ToList() in your Controller as your context as been disposed at the end of the using block.

    In your DL method, just do something like:

    IEnumerable function()
    {
        using(DBContext context = new DBContext())
        {
          return something.ToList();
        }
    }
    

    and in your Controller you'll get an IEnumerable of Something:

    var mySomethingIEnumerable = DL.Function();
    

    Hope that helps!

提交回复
热议问题