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

Comments

  • Anonymous
    May 12, 2009
    PingBack from http://asp-net-hosting.simplynetdev.com/sneak-preview-deferred-loading-in-entity-framework-40/

  • Anonymous
    May 12, 2009
    Ever wondered about what is POCO? POCO stands for Plain Old CLR Object which does not have any persistence

  • Anonymous
    May 12, 2009
    Thank you for submitting this cool story - Trackback from DotNetShoutout

  • Anonymous
    May 12, 2009
    Thank you for submitting this cool story - Trackback from progg.ru

  • Anonymous
    May 12, 2009
    Really promising, I will never lose the faith in EF although I had some hard times with v1. Seems that you really doing great job. I also have few questions regarding LINQ to Entities not related to deferred loading. Like supporting for Cast<T> so far when my classes implement ISomeInterface and I call Cast<T> it throws NotSupportedException. To workaround this I have to call AsEnumerable before it. And may other things like support for Enums, custom extension methods call withing from or within linq queries etc... Thank you for the cool snippets

  • Anonymous
    May 12, 2009
    Deferred Loading is really cool future. But could you please make it more granular. Having attributes on class properties will bring more power in controlling deferred loading. Sure, the EDM designer should support it. Generated entity reference should have field to control deffered loading also, so that for some classes we could enable/disable deffered loading at runtime. Having just switcher on/off will exclude all scenarios where huge and rarely used classes are mixed with small and often used one.

  • Anonymous
    May 13, 2009
    I'm wondering how deffered loading is supposed to work for POCO objects? I mean, you somehow need to attach some logic to the POCO object itself, don't you? Are there any new language features in .NET 4.0 that allow you to do that?

  • Anonymous
    May 13, 2009
    I have started talking to developers about what they can expect in Entity Framework V2 such as in my

  • Anonymous
    May 13, 2009
    The comment has been removed

  • Anonymous
    May 13, 2009
    The comment has been removed

  • Anonymous
    May 13, 2009
    The comment has been removed

  • Anonymous
    May 13, 2009
    What to be Expecting of Entity Framework in .NET 4 The ADO.NET team started to release a series of posts

  • Anonymous
    May 14, 2009
    Windows Azure/Azure Service Framework (including .NET Services)/BizTalk David Pallmann has released 2.1 of Azure Storage Explorer with the new feature of modifying what is in storage including create or delete blob containers, blob items, queues, queue

  • Anonymous
    May 14, 2009
    W00t!  Sure makes migrating from Linq2Sql easier! :)

  • Anonymous
    May 16, 2009
    I like the fact that even though i have deferred loading enabled, I can still use CreateSourceQuery and not cause the lazy loading to occur when i do customer.Order.CreateSourceQuery(o => o.ShipCity == "London")); I have not tried this but how does the MergeOption work when lazy loading happens? in v1, if the parent entity is loaded with noTracking calling Load with no option causes the child collection to be loaded using NoTracking as well. Is this behavior same for lazy loading? Zeeshan Hirani

  • Anonymous
    May 18, 2009
    What about having something in the web.config to set default behaviour of DefferedLoading (true or false)?

  • Anonymous
    May 20, 2009
    @Zeeshan Lazy loading will work the same as Load. If you are tracking, the MergeOption will be AppendOnly. If you are "no tracking" (returns from a MergeOption.NoTracking query), then lazy load will use MergeOption.NoTracking to keep things consistent. Jeff

  • Anonymous
    May 26, 2009
    As i see it the only way to enable lazy load is to set it on the context, but I would really like to have a per navigation property enabling of lazy load - will that be possible?

  • Anonymous
    May 28, 2009
    In my post last week on the POCO Experience in Entity Framework , I covered the fundamentals of POCO

  • Anonymous
    May 30, 2009
    Last week I mentioned in the sneak preview on POCO that support for POCO entities is one of the new capabilities

  • Anonymous
    May 30, 2009
    【译者按】 Entity Framework 1.0 发布也有一段时间了,但感觉用的人很少。其中一个很大的原因,也许就是不支持POCO。要知道,Entity Framework 1.0的做法是让你的实体从EF的基类继承而来

  • Anonymous
    June 03, 2009
    Introduction: From the moment I put my hands on Visual Studio.Net 2010 Beta 1 and I’m targeting EF4

  • Anonymous
    June 10, 2009
    &#160; Entity Framework 4.0 Beta 1(又称EF V2)与 .NET 4.0 Beta 1 一起发布,包含了一系列的重要改进:自定义代码生成、延迟加载、N层支持、POCO支持

  • Anonymous
    June 28, 2009
    あなたの精神年齢を占ってみよう!当サイトは、みんなの「精神年齢度」をチェックする性格診断のサイトです。精神年齢度には、期待以上の意外な結果があるかも??興味がある方はぜひどうぞ

  • Anonymous
    June 29, 2009
    There is one big gap still exists which is needed to be filled,will we able to retrieve data using stored procedures in eager-loading approach.I mean,if we've two entities A and B and I want to get data from these entities using stored procedures(not using Include method) then how I would be able to do that?

  • Anonymous
    July 19, 2009
    I wonder if we can say somewhere in the model that an specific field (a BLOB for example) should be deferred. Also wonder, if there shoundn't be an web.config option to set DeferredLoadingEnabled == True by default. Thanks,

  • Anonymous
    August 20, 2009
    Will EF 4.0 support ghosting?  In which you may want to load the id's and name field for the child objects but not all of the attributes of the child objects?

  • Anonymous
    July 21, 2011
    Has there any update on the .NET Framework?