Today I tried to modify my existing generic GetAll method to work with eager includes.
I have Lazy loading enabled, but I want to do eager loading for some specific caching optimizations.
public IQueryable<T> GetAll<T>(List<string> eagerIncludeList) where T : EntityObject, new()
{
var entitySet = String.Format("[{0}]", FetchEntitySetName<T>());
ObjectQuery<T> q = this.db.CreateQuery<T>(entitySet);
foreach (string x in eagerIncludeList) {
q.Include(x);
}
return q;
}
The above code doesn’t work (not eager loaded at least) , calling it with e.g.
DB.GetAll<PageConfig>(new List<string>() {"Containers"})
does not load the related Containers for my PageConfigs,
BUT
When hard coding the include, it does work, so i’m sure the name “Containers” is correct
var example = db.PageConfigSet.Include("Containers").ToList();
this gives example.First().Containers.IsLoaded = true;
Any ideas on why the generic code that uses CreateQuery doesn’t work, but the one that uses the hard coded ObjectSet does?
q.Include(x)returns a newObjectQuery<T>that includes that table.You aren’t doing anything with the return value.