Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use entity framework with hierarchical data?

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.

like image 498
jamesfm Avatar asked May 01 '09 16:05

jamesfm


1 Answers

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.

like image 131
Georg Wächter Avatar answered Oct 21 '22 23:10

Georg Wächter