Sneak Preview: Deferred Loading in Entity Framework 4.0

Published 12 May 09 05:20 PM | dpblogs 

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

Filed under:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# Sneak Preview: Deferred Loading in Entity Framework 4.0 | ASP NET Hosting said on May 12, 2009 9:13 PM:

PingBack from http://asp-net-hosting.simplynetdev.com/sneak-preview-deferred-loading-in-entity-framework-40/

# Sriram's WebLog on ASP.NET VB.NET C# said on May 12, 2009 9:57 PM:

Ever wondered about what is POCO? POCO stands for Plain Old CLR Object which does not have any persistence

# DotNetShoutout said on May 12, 2009 10:00 PM:

Thank you for submitting this cool story - Trackback from DotNetShoutout

# progg.ru said on May 12, 2009 11:47 PM:

Thank you for submitting this cool story - Trackback from progg.ru

# Muhammad Mosa said on May 13, 2009 2:18 AM:

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

# Andrew Veresov said on May 13, 2009 5:08 AM:

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.

# Robert said on May 13, 2009 9:11 AM:

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?

# IUpdateable from Eric Nelson said on May 13, 2009 1:01 PM:

I have started talking to developers about what they can expect in Entity Framework V2 such as in my

# Pedro Brown said on May 13, 2009 6:18 PM:

Hi,

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,

Pedro Brown.

# Jason Short said on May 13, 2009 6:58 PM:

I agree with Pedro above.  Specific fields being deferred is huge.  BLOBs can take a large hit at load time and right now the only way to delay them is to move them to another table and not map it.

# Daniel Simmons said on May 13, 2009 11:51 PM:

@Robert,

Deferred loading can be combined with POCO classes if you mark your navigation properties (collections or references) as virtual and don't seal your entity class.  Then the system will automatically supply a proxy class which inherits from your POCO class and performs defferred loading for you.

@Pedro & Jason,

Unfortunately deferred loading of blobs is not something that made it into the schedule for EF4.  It's definitely something that we want to do and will do in a future release, but it just didn't make it for this round.

- Danny

# Gil Fink on .Net said on May 14, 2009 3:32 AM:

What to be Expecting of Entity Framework in .NET 4 The ADO.NET team started to release a series of posts

# Sam Gentile's Blog (if (DeveloperTask == Communication && OS == Windows) said on May 14, 2009 1:48 PM:

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

# shawn said on May 14, 2009 9:33 PM:

W00t!  Sure makes migrating from Linq2Sql easier! :)

# Zeeshan Hirani said on May 17, 2009 12:05 AM:

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

# Pedro Brown said on May 18, 2009 7:09 PM:

What about having something in the web.config to set default behaviour of DefferedLoading (true or false)?

# Jeff said on May 21, 2009 2:44 AM:

@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

# Joachim Lykke Andersen said on May 26, 2009 4:44 PM:

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?

# ADO.NET team blog said on May 28, 2009 12:04 PM:

In my post last week on the POCO Experience in Entity Framework , I covered the fundamentals of POCO

# Web开发技术 said on May 30, 2009 10:16 PM:

Last week I mentioned in the sneak preview on POCO that support for POCO entities is one of the new capabilities

# Web开发技术 said on May 30, 2009 11:50 PM:

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

# VS2010学习 said on June 3, 2009 11:43 AM:

Introduction: From the moment I put my hands on Visual Studio.Net 2010 Beta 1 and I’m targeting EF4

# VS2010学习 said on June 11, 2009 1:33 AM:

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

# 精神年齢 said on June 29, 2009 12:12 AM:

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

# Tahir said on June 30, 2009 3:24 AM:

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?

# radyo dinle said on July 19, 2009 10:42 AM:

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,

# Jeff C said on August 20, 2009 2:16 PM:

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?

Leave a Comment

(required) 
(optional)
(required) 

  
Enter Code Here: Required

Search

This Blog

Syndication

Page view tracker