在实体框架中使用一个DBContext只连接一次数据库的多个Linq到实体查询吗

时间:2021-11-08 09:37:05

I am trying to return two result sets from an SQL Server database using Entity Framework 6. I would like to try this by running 2 Linq to Entity queries using a single DBContext. My question is by using a single DBContext is whether my request is only being hit by a database connection once. I think it is but I am not sure.

我正在尝试使用Entity Framework 6从SQL Server数据库返回两个结果集。我想通过使用单个DBContext运行2个Linq到实体查询来尝试这一点。我的问题是,通过使用一个DBContext,我的请求是否只被一次数据库连接击中。我想是的,但我不能肯定。

class RequestRefLists
{
  public List<Employee> EmployeeList {get;set;}
  public List<Dept> DeptList {get;set;}
}

    public RequestRefLists GetRequestRefLists()
   {
    RequestRefLists ReqRefLists = new RequestRefLists();

    using(var context= new BusinessDBContext())
    { 
      var queryResult1 = from e in context.Employees
      select e;
      ReqRefLists.EmployeeList = (List<Employee>)queryResult1.ToList();

      var queryResult2 = from d in context.Departments
      select d;
      ReqRefLists.DeptList = (List<Dept>)queryResult2.ToList();
    }
    return ReqRefLists;
   }

1 个解决方案

#1


4  

You can use Entity Framework Extended Library.

您可以使用实体框架扩展库。

There is a feature named Future queries

有一个名为Future查询的特性

class RequestRefLists
{
    public List<Employee> EmployeeList {get;set;}
    public List<Dept> DeptList {get;set;}
}

public RequestRefLists GetRequestRefLists()
{
    RequestRefLists ReqRefLists = new RequestRefLists();

    using(var context= new BusinessDBContext)
    { 
        var queryResult1 = from e in context.Employees
        select e;
        ReqRefLists.EmployeeList = queryResult1.Future();

        var queryResult2 = from d in context.Departments
        select d;
        ReqRefLists.DeptList = queryResult2.Future();        
    }
    return ReqRefLists;
}

Your queries will execute lazy on first enumeration of any collection.

您的查询将在第一次枚举任何集合时执行惰性。

ExecuteFutureQueries builds a batch query from all the stored IFutureQuery objects. Finally, all the IFutureQuery objects are updated with the results from the query.

ExecuteFutureQueries从所有存储的IFutureQuery对象构建一个批处理查询。最后,所有IFutureQuery对象都使用查询的结果进行更新。

#1


4  

You can use Entity Framework Extended Library.

您可以使用实体框架扩展库。

There is a feature named Future queries

有一个名为Future查询的特性

class RequestRefLists
{
    public List<Employee> EmployeeList {get;set;}
    public List<Dept> DeptList {get;set;}
}

public RequestRefLists GetRequestRefLists()
{
    RequestRefLists ReqRefLists = new RequestRefLists();

    using(var context= new BusinessDBContext)
    { 
        var queryResult1 = from e in context.Employees
        select e;
        ReqRefLists.EmployeeList = queryResult1.Future();

        var queryResult2 = from d in context.Departments
        select d;
        ReqRefLists.DeptList = queryResult2.Future();        
    }
    return ReqRefLists;
}

Your queries will execute lazy on first enumeration of any collection.

您的查询将在第一次枚举任何集合时执行惰性。

ExecuteFutureQueries builds a batch query from all the stored IFutureQuery objects. Finally, all the IFutureQuery objects are updated with the results from the query.

ExecuteFutureQueries从所有存储的IFutureQuery对象构建一个批处理查询。最后,所有IFutureQuery对象都使用查询的结果进行更新。