It seems like LINQ to SQL has some support for eager loading. Let's say I wanted to load a blog post with all of its comments, for example:
var options = new DataLoadOptions();
options.LoadWith<Post>(p => p.Comments);
dataContext.LoadOptions = options;
This works fairly well, but it feels a little strange to be defining this on the datacontext level. If you use the datacontext as a unit of work that's shared between repositories, then you might run into a situation where one query needs the comments eager-loaded and the other doesn't.
LINQ to SQL's eager-loading capabilities don't seem to support that. It also seems to throw an error if you try to set the datacontext's LoadOptions
after a query has already been executed against that particular datacontext, which means that the eager-loading needs to be set at a higher level that's aware of every database query that will be run during the unit of work.
Am I missing something painfully obvious here? It feels like there should be a way to specify what entities should be eager-loaded on a per-query basis as opposed to setting it globally for the entire datacontext...