Tree stucture table and entity framework

前端 未结 1 2009
梦如初夏
梦如初夏 2021-01-14 07:51

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.

1条回答
  •  囚心锁ツ
    2021-01-14 08:08

    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.

    0 讨论(0)
提交回复
热议问题