LinqDataSource doesn't load child tables

FYI, there’s a bug in the RTM LinqDataSource where child tables aren’t loaded unless you have updating or deleting enabled. Apparently if you have a LinqDataSource that doesn’t have updates or deleted enabled, ObjectTracking is turned off (for performance reason), but deferred queries (e.g. queries to pull back child rows) aren’t executed.

So statements like

<%# Eval("ChildTable.ChildTableField") %>

in your ListView, etc. won’t work.

Annoying, needless to say. But at least now I know.

So, there are a few options:

  1. Set EnableUpdate=”true”, which will enable ObjectTracking and cause deferred updates to work
  2. Add a SELECT statement in your LinqDataSource (e.g. SELECT=”new (field1, field2, childTable)”) to pull back everything you need.
  3. Handle the ContextCreated event to either manually set ObjectTrackingEnabled, or manually set LoadOptions.
public void OnContextCreated(object sender, LinqDataSourceContextEventArgs e) {
  ((DataContext)e.ObjectInstance).ObjectTrackingEnabled = true;
}

OR

public void OnContextCreated(object sender, LinqDataSourceContextEventArgs e) {
    var dataLoadOptions = new DataLoadOptions();
    dataLoadOptions.LoadWith<MyTable>(t => t.ChildTable);
    ((DataContext)e.ObjectInstance).LoadOptions = dataLoadOptions;
}

Setting LoadOptions is normally the best approach for performance.

0