Sneak Preview: Deferred Loading in Entity Framework 4.0

 


The information in this post is out of date.

Visit msdn.com/data/ef for the latest information on current and past releases of EF.


 

After our first release, one of the common pieces of feedback we heard from you was that you would like to be able to get automatic deferred/lazy loading of related entities much like the support for deferred loading in LINQ to SQL.

I am happy to say that we now have support for automatic Deferred Loading in Entity Framework 4.0. Going forward, you will be able to write code like this with the Entity Framework:

 using (

NorthwindEntities

  db = new 

NorthwindEntities

 ())
{
    db.ContextOptions.DeferredLoadingEnabled = true;

    foreach (

Customer

  c in db.Customers)
    {
        if (c.Orders.Count > 10)
        {
            SendLoyaltyRewardToCustomer(c);
        }
    }
}

In this example, there are a few things to note:

  • Note that I am not doing anything “explicitly” in my code to load the Orders for each customer. I can simply use that property (in this case I am looking up the order count for each customer).
  • Deferred Loading is enabled on the context through the DeferredLoadingEnabled property.
  • When DeferredLoadingEnabled is set, you no longer call Load explicitly on the Orders navigation property in order to load the related orders.
  • Deferred Loading works with code-generated entities as well as POCO entities.
  • Deferred Loading is turned off by default. However, there is flexibility to tweak this to your needs by using customizable code generation.

Keep in mind that this is a simple example of how to use Deferred Loading. I will be covering more about Deferred Loading and related aspects in my in-depth discussion of POCO next week.

Let us know what you think, and stay tuned!

- Faisal Mohamood
Program Manager, Entity Framework