Loading a self-referencing table with zeal

c# entity-framework hierarchical-data


I maintain a typical self-reference table ofCategories My entity model has associations that I've created.Children and Parent Can the whole game be loaded at once?Category 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.Children numerous properties


But this doesn't achieve what I want and creates a ton of crazy T-SQL code.

5/7/2015 9:59:16 AM

Accepted Answer

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.

9/14/2009 2:15:52 PM

Popular Answer

Okay, you may think about utilizing the load approach.

 if (!category.Children.IsLoaded)

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.

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow