Saturday, September 15, 2007

DataLoadOptions and non-default behavior

I guess this is a stupid developer trick, but I've made the mistake myself and have seen several others do the same. What's wrong with this code?


using(MyDataContext dc = new MyDataContext())
{
DataLoadOptions dlo = new DataLoadOptions();
dlo.LoadWith(m=>m.MyBars);

var query = dc.MyFoos.Where(m=>m.SomeValue == 42);

return query.ToList();
}


It's the missing "dc.LoadOptions = dlo" call. Surprising to me that the "LoadOptions" property on the DataContext doesn't just have an already-created DataLoadOptions. I'm sure there's a good reason for this (something to do with the immutability of that object and all), but it seems to be a common mistake around here anyway, and non-obvious, especially if the DataLoadOptions setter is stuck back in a DataContext factory somewhere. It's happened enough that "did you set the LoadOptions property?" has become the first question I ask when someone complains of a particular child object value being null.

No comments: