views:

407

answers:

1

How can I lazy load an association (EntitySet) in LINQ to SQL? You can't set Delay Loaded on Associations in the designer, and I couldn't find a DBML attribute for it either. I looked at DataLoadOptions to see if there was a way to lazy load them that way, but DataLoadOptions really just provides a way to mold the SQL that is generated for properties and associations (and force loading of lazy loaded properties).

This is a very important feature for us, as we have a pretty heavy object graph that gets pushed to the DB. When loading some of the top level objects, I don't want to load their associated sub-entities until the user actually requests that data. It's easy to do for individual properties, but I can't find a way to do it for associations. The EntitySet collection seems to support the concept, so there has to be a way.

+3  A: 

Lazy loading is enabled by default in Linq to SQL - EntitySet and EntityRef are used support this feature.

Michael Gattuso
Interesting. Is this reflected anywhere in the documentation? It makes sense that EntitySet would lazy load by default, but I don't see it anywhere in the docs.
Matt Holmes
Microsoft calls it "Deferred Loading" instead of Lazy Loading. Look in the following MS documentation for Linq to SQL. http://msdn.microsoft.com/en-us/library/bb425822.aspxand do a text search for "deferred loading" (you will also find deferred execution which is something else)
Michael Gattuso