In Entity Framework 4, what is the difference between Lazy Loading, and using Load() method?
Edit: I have added, two ‘if’ statements:
Lazy Loading:
var query = from c in context.Contacts select c;
foreach ( var contact in query ) {
if ( contact.ID == 5 )
Console.WriteLine( contact.Addresses.City );
}
Load() method:
context.ContextOptions.LazyLoadingEnabled = false;
var query = from c in context.Contacts select c;
foreach ( var contact in query ) {
if ( contact.ID == 5 ) {
contact.Addresses.Load()
Console.WriteLine( contact.Addresses.City );
}
}
Now, having this two ‘if’ checks, why should I preffer one before another?
Lazy Loadingmeans that a load will only occur once the object is needed, thus not loading unnecessary data.When you disable
Lazy Loadingyou say that you will load yourself by calling load.http://en.wikipedia.org/wiki/Lazy_loading
Lazy Loadingis disabled by default, so when you set it tofalsein your first line it does not do anything.When you call
Load, you will load all the related objects to that database (which is not needed in this case which makes it work without it)