I have been tasked with building a new system that models a virtual filesystem. My client has mandated that this be built using Entity Framework. I have handled node-like data structures before, but never with Entity Framework.
What is the best approach to build a hierarchical class in Entity Framework? By hierarchical, I mean that a class can have a parent of the same type, and can have zero or more children of the same type.
I’m using SQL Server 2008 and Entity Framework 4.0. Should I use the built-in hierarchy data type, or do I go the ParentID route? Suggestions would be most welcome.
I had the same problem. I found that the best way to work with hierarchyid datatype and still be using EF 4.0 is building a view over the the hierarchy table.
Since the view is not updateable I created a delete, insert and add stored procedures and mapped them to the entity mapping in the ORM. This is working really good.
Lets say you have this table:
Now you create this view over it:
Now I can create an entity in the model over the view and use Linq-to-Entities and get good performace and neat code.
This is the add stored procedure I use:
I created all the needed indexes and constraints over the tables. In my solution the view is displaying data from other tables and the procedures manipulate these tables as well.
Oded