7

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.

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
  • A simple glance at the executed SQL statements could have answered your question. Commented Dec 24, 2015 at 8:50

1 Answer 1

8

You can use Entity Framework Extended Library.

There is a feature named Future queries

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Question is do entity framework hit database only once when using same dbcontext object for multiple linq.
@SundarSingh and Future() does exactly this.
If I understand it correctly it appears you can stack your queries within a single dbcontext each with the word .Future at the assignment point. In my case that is where I want to load a particular list in a custom class. After all queries have been defined I just enumerate one of them and all the assignments to my class instance are supposed to be performed in a single database connection. I'll test this. I am trying to load all the lookup lists from tables for a UI. I wonder if the extensions are officially part of Entity Framework. Thanks Vadim.
There is no built mechanism to combine multiple queries in one batch. It is the reason of existence of EF Extended Library. Why do not you want to include 1 nuget package?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.