I maintain a typical self-reference table of
My entity model has associations that I've created.
Can the whole game be loaded at once?
without lazy loading on an object?
If I use the following code, just the second level loads.
db.Categories.MergeOption = System.Data.Objects.MergeOption.NoTracking; var query = from c in db.Categories.Include("Children") where c.IsVisible == true orderby c.SortOrder, c.Id select c;
If I already have all the category objects loaded, can I load references?
Adding the data is one way to load it.
But this doesn't achieve what I want and creates a ton of crazy T-SQL code.
It is not feasible. Think about this: Every LINQ to Entities query is converted to SQL. Which SQL query uses a self-referencing hierarchy with an infinite depth? Standard SQL doesn't have one. I'm not aware of any T-SQL extensions for this, and I doubt that EF vendors are either.
Okay, you may think about utilizing the load approach.
if (!category.Children.IsLoaded) category.Children.Load();
Of course, ObjectContext must keep track of category entities.
Here, how-does-entity-framework-work-with-recursive-hierarchies-include-seems-not-to, a better explanation is available.