Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework 4 selective lazy loading properties

People also ask

Does Entity Framework support lazy loading?

Entity Framework supports three ways to load related data - eager loading, lazy loading and explicit loading.

Which entity will allow lazy loading?

Yes, lazy loading is enabled in the Entity Framework ORM too, it is on by default in Entity Framework, so if you want to enable lazy loading in Entity Framework, you don't need to do anything.

How do I set lazy loading in Entity Framework?

Lazy loading means delaying the loading of related data, until you specifically request for it. When using POCO entity types, lazy loading is achieved by creating instances of derived proxy types and then overriding virtual properties to add the loading hook.


Now that you have read everyone's reply, I will give you the correct answer. EF does not support lazy loading of properties. However it does support a much powerful concept then this. It's called table splitting where you can map a table to two entities. Say a product table in the the database can be mapped to product entity and ProductDetail entity. You can then move the expensive fields to the ProductDetail entity and then create a 1..1 association between prodcut and productdetail entity. You can then lazy load the productdetail association only when you need it. In my performance chapter of my book, I have a recipe called. 13-9. Moving an Expensive Property to Another Entity

Hope that helps!

Julie Lerman has an article on how to split a table


With a scalar property, the only way to selectively not load a certain property is to project in ESQL or L2E:

var q = from p in Context.People
        select new
        {
            Id = p.Id,
            Name = p.Name // note no Biography
        };

+1 to Dan; doing this lazily is worse than loading it up-front. If you want to control loading, be explicit.


stimms is correct, but be careful while using lazy loading. You may have performance issues and not realize the property is getting loaded at a specific location in your code. This is because it loads the data when you use the property

I prefer to use explicit loading. This way you know when they get loaded and where. Here's a link that gives an example for the LoadProperty http://sankarsan.wordpress.com/2010/05/09/ado-net-entity-framework-data-loading-part-2/

You can also you Eager Loading by using the Include method. Example here:http://wildermuth.com/2008/12/28/Caution_when_Eager_Loading_in_the_Entity_Framework


Given a query over an EntityFramework DbSet, where the targeted entity contains a BigProperty and a SmallProperty, When you're trying to only access the SmallProperty without loading the BigProperty in memory :

//this query loads the entire entity returned by FirstOrDefault() in memory
//the execution is deferred during Where; the execution happens at FirstOrDefault
db.BigEntities.Where(filter).FirstOrDefault()?.SmallProperty;

//this query only loads the SmallProperty in memory
//the execution is still deferred during Select; the execution happens at FirstOrDefault
//a subset of properties can be selected from the entity, and only those will be loaded in memory
db.BigEntities.Where(filter).Select(e=>e.SmallProperty).FirstOrDefault();

Therefore you could exploit this behaviour to only query the BigProperty where you actually need it, and use select statements to explicitly filter it out everywhere else.

I tested this with the Memory Usage functionality from the Visual Studio debug Diagnostic Tools.