Sql – LINQ to SQL: Lazy loading an association

linqlinq-to-sqlnetsql

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.

Best Answer

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