Tree stucture table and entity framework
I have tree structure table like,
Table{Id, ParentId, Name}
where Id is primary key, and parent Id is a foreign key, but to the same table. It will have values like
Id ParentId Name
1 - 开发者_高级运维 N1
2 1 N2
3 1 N3
4 2 N4
here, N2 and N3 is child of N1, and N4 a child of N2
I am using Entity framework and MVC to build rest like webservice.
How can I use entity framework to recursively to get all child nodes?
How can I use entity framework to recursively to get all child nodes?
EF doesn't have support for recursive queries. You can map such structure easily as referenced by @Eranga in comment but once you load entities you must either manually define eager loading (= you must say how deep it should go) or your child entities will be loaded by lazy loading first time they are accessed (that is extremely inefficient).
The best way to work with hierarchical structures in EF is either using database view to hide hierarchical SQL query or using SQL / stored procedures directly.
精彩评论