Eager Loading child entities in Entity Framework

Once again I am comparing different behaviors in linq to SQL and entity framework. If you want to eagerly load child entities in Linq to SQL, you would use LoadWith operator available on DataLoadOptions. LoadWith method retrieves related objects for the main object. You have the option to call LoadWith method as many times as you need depending upon how many related objects you want to eagerly load.  If you want to eagerly load related objects in entity framework, you you can call include method. Include takes a path which represents the name of the related entity that you want to load. In the path parameter you can also use traversal by using dot notations. For instance for a particular customer, if you want to load its Orders and for each order want to load its OrderDetails, instead of making of two Include calls you can call include with a traversal like cust.Include("Orders.OrderDetails"). This tells entity framework to load Orders and for each Order load its OrderDetails as well. As with linq to SQL, you also can make more than 1 calls to Include to load multiple entities at the same time. Below is an example that illustrates different usage of eager loading with entity framework and linq to SQL.

image

In the above example, I start off with showing how to eagerly load child collections with linq to SQL. I am using DataLoadOptions class and calling LoadWith method to load its Orders and for each order load its Order Details. I am also loading the addresses for the customer as well. Notice that I had to make use of LoadWith method 3 times but all my code is strongly type and I am not having to type in strings in the load with parameter. Instead I make use of lambda expressions to specify what sub collections to load.

Now coming back to entity framework, in order to eagerly load sub collections I am using Include operator. Notice in order to load order and its order detail, I am not calling Include operator several times. Instead I am traversing the entity path which tells entity framework that I want to load order and its order details. As I mentioned earlier, you have the option to call Include several times, therefore I am calling Include again to load addresses for the customer as well. Over all I think Include syntax is better than Linq's LoadWith except Include is not strongly type meaning my string does not evaluate until runtime and I don’t get design time compilation which is offered by LoadWith operator in Linq to SQL.

Below is the output from the above query. The results from linq to SQL and entity framework is the same.

image

Published Sunday, July 13, 2008 6:36 PM by zhirani

Comments

# re: Eager Loading child entities in Entity Framework

Wednesday, July 28, 2010 2:15 PM by weitzhandler

It's a flop that there is no strongly typed "Load with" in Entity Framework.

Dim x = db.Customers.Include(Of blah blah) some nicer way to add path rather than hard coded string).

# re: Eager Loading child entities in Entity Framework

Wednesday, February 9, 2011 6:58 PM by Omar

Thanks a lot it solved a big problem to mee :D