Geeks With Blogs

News

This blog has moved to ericnelson.wordpress.com

 Subscribe in a reader

Add to Google Reader or Homepage


Links
View my teams slideshare
These postings are provided "AS IS" with no warranties, and confer no rights.



IUpdateable has moved to ericnelson.wordpress.com Please visit http://ericnelson.wordpress.com

[This is the fifth in a series of posts on getting started with the new features in Entity Framework 4 based on the demos I did in my session at TechEd Europe in Berlin last week (Nov 2009).]

Entity Framework v1 did not support a commonly implemented ORM feature called Lazy Loading. Entity Framework 4 adds that feature. In Beta 1 it was known (confusingly IMHO) as Deferred Loading. In Beta 2 it is (thankfully IMHO) known as Lazy Loading.

You run up against the absence of Lazy Loading in v1 when you start moving across associations between Entities (Objects). Imagine for instance you had an instance of Order from Northwind and wanted to access its Order_Details, perhaps to do something as simple as return a count of the number of Order_Details.

You would be tempted to write:

order.Order_Details.Count

However this will return 0 unless you have already explicitly loaded the related Order_Details into the ObjectContext.

In v1 you could either explicitly project in your query (stating up front that you were interested in the count of Order_Details) or you could use explicit loading :

Orders.Include(“Order_Details”)

Either approach would ultimately give you the correct value instead of a dreaded 0.

However Lazy Loading ensures that in situations like the above, “It just works” and the beauty is that it requires a single statement to turn it on.

context.ContextOptions.LazyLoadingEnabled = true;

The following example performs the same operation, first with Lazy Loading off, then with it on:

image

Will product the output:

image

In a future post I will talk about the hidden dangers of LazyLoading and compare it to other ORM implementations.

Posted on Thursday, November 26, 2009 10:23 AM .NET 4.0 , Data , Entity Framework , EF4 | Back to top


Comments on this post: Getting Started with Entity Framework 4 – Lazy Loading

# re: Getting Started with Entity Framework 4 – Lazy Loading
Requesting Gravatar...
Lazy loading is enabled in EF4 by deafult(context.ContextOptions.LazyLoadingEnabled returns "true"). What that means when you do order.Order_Details.Count, It WOULD GIVE you the count. Also, you can iterate through order you WILL get the Order details for each order.

In order to switch off lazy loading, you need to set context.ContextOptions.LazyLoadingEnabled to false. If you want to switch off lazy loading for the EntityContainer as a whole so that you sont have to set it again and again, you should use use the following in the EDMX file:-

<EntityContainer Name="MyEntitiesContext" annotation:LazyLoadingEnabled="false">

A very good explaination of the same is here:- http://stackoverflow.com/questions/2967214/disable-lazy-loading-by-default-in-entity-framework-4
Left by Ashish Gupta on Aug 14, 2010 7:56 AM

Your comment:
 (will show your gravatar)


Copyright © Eric Nelson | Powered by: GeeksWithBlogs.net | Join free