Eager Deferred Loading of related entities

“Eager Deferred” is a bit of an oxymoron, but let me explain.

Example:

You have queried for customers.

For particular customers you want to get their orders AND order details.

customer.Orders.Load will only get the order entities.

But what if you wanted to do something similar to Include in there?

eg customer.Orders.Load.Include(“Details”)

That syntax is not possible. and is something Ben S added to the Entity Framework v2 Wish List started by John Papa.

Here’s how to pull it off in V1, as explained by Danny Simmons.

customer.Orders.Attach(customer.Orders.CreateSourceQuery().Include(“Address”))

It’s logical and it works, but it’s awfully convoluted. Two years of futzing with Entity Framework and 300 pages into my book and yet, here is something I have never seen or thought up before. And while it’s a neat trick, it’s still pretty hard to get to.

I much prefer the Load.Include() idea. It’s discoverable and feeds on something we already know how to do. But for now, at least I know how to do it in V1.

There’s so much in V1 and for many scenarios where you think you are stuck, whether it’s with the model or with code, as with Danny’s example, EF and EDM are robust enough that there is generally SOME way to pull it off. But it takes a good understanding of the APIs or someone like Danny sitting by your side to figure some of these things out.

Another great example is Jarek Kowalski’s code to perform transparent Lazy Loading in EF. Lack of lazy loading has been an EF showstopper for some, but it is possible to add it in.

More of this functionality will be hidden under the covers in V2 with much simpler ways to tap into it (at least I”m assuming and counting on this for V2). For now, folks (on the team and in the community) are writing extensions and code to make many tasks easier to perform. Don’t forget to check out the team’s code gallery (http://code.msdn.microsoft.com/adonetefx as well as the www.codeplex.com/EFContrib site.

  Sign up for my newsletter so you don't miss my conference & Pluralsight course announcements!  

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.