How do I use entity framework with hierarchical data?

jamesfm picture jamesfm · May 1, 2009 · Viewed 7.2k times · Source

I'm working with a large hierarchical data set in sql server - modelled using the standard "EntityID, ParentID" kind of approach. There are about 25,000 nodes in the whole tree.

I often need to access subtrees of the tree, and then access related data that hangs off the nodes of the subtree. I built a data access layer a few years ago based on table-valued functions, using recursive queries to fetch an arbitrary subtree, given the root node of the subtree.

I'm thinking of using Entity Framework, but I can't see how to query hierarchical data like this. AFAIK there is no recursive querying in Linq, and I can't expose a TVF in my entity data model.

Is the only solution to keep using stored procs? Has anyone else solved this?

Clarification: By 25,000 nodes in the tree I'm referring to the size of the hierarchical dataset, not to anything to do with objects or the Entity Framework.

Answer

Georg Wächter picture Georg Wächter · May 12, 2009

It may the best to use a pattern called "Nested Set", which allows you to get an arbitrary subtree within one query. This is especially useful if the nodes aren't manipulated very often: Managing hierarchical data in MySQL.

In a perfect world the entity framework would provide possibilities to save and query data using this data pattern.