views:

270

answers:

4

Is it possible to load an entity excluding some properties? One of this entity's properties is expensive to select. I would like to lazy load this property. Is that possible?

A: 

I believe the default behaviour in EF is to not load relations by default. If you do load the relations then you can set

context.ContextOptions.LazyLoadingEnabled = true;

http://msdn.microsoft.com/en-us/library/bb896249.aspx

stimms
stimms and Dan,I don't mean a related property, I am referring to a field (class member) like Biography (string in C# or varchar(1024) in SQL Server ) which is really expensive to load with the entity, I would like to lazy load it when necessary. As far as I know, this does not work in EF4. Is there a way to do it?
Nazaf
+1  A: 

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

Dan H
stimms and Dan,I don't mean a related property, I am referring to a field (class member) like Biography (string in C# or varchar(1024) in SQL Server ) which is really expensive to load with the entity, I would like to lazy load it when necessary. As far as I know, this does not work in EF4. Is there a way to do it?
Nazaf
So you want to lazy load a scalar property? Just off of the top of my head I might try splitting Biography out into it's on Entity and the setting it as a navigation property. Then I'd be able to lazy/eager/expicit load it. There may be a better way...I'll test this theory when I get a chance.
Dan H
Thanks, it seems to be the only efficient way.
Nazaf
A: 

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.

Craig Stuntz
Thanks for this suggestion. Yes, but this projection is not efficient. First, it would result in an anonymous type which cannot be used as People directly. A new People object has to be created and instantiated properly, which will be waste of time parsing between objects. This type won't be trackable either by the context.
Nazaf
@Nazaf, you are prematurely optimizing.
Craig Stuntz
+1  A: 

Now that you have read everyone's reply, i will give u the correct answer. EF does not support lazy loading of properties. However it does support a much powerful concept then this. Its 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!

zeeshanhirani
Thanks. I'll have to do this. But EF4 should support lazy loading on scalar properties, it can be really handy.
Nazaf
I know the team is working on supporting out of the box lazy loading properties in the next version but i do think moving expensive columns to another entity opens up avenues to delay loading several expensive properties at once. Imagine you have an employee object with EmployeePicture and employeedescription which are both expensive and you want to delay load both of them but whenever you want to load them, you want to load them together. This is something that you can't do in linq to sql.
zeeshanhirani
Yes, if there are more than one expensive field, in this case EF team can enable a flag like LoadAllLazyProperties which allows EF to load all scalar properties marked as Lazy Load together, whenever any of them is requested. This improves situations like the one you mentioned without separating EntityTypes!!
Nazaf

related questions