Support for querying a hierarchy of self-referencing entities?
See original GitHub issueHi,
Often you need to retrieve a whole hierarchy of self-referencing entities.
Consider this example:
public class Affiliate
{
public int Id { get; set; }
public string Name { get; set; }
//The person who referred the current one
public in ReferrerId { get; set; }
[ForeignKey(nameof(ReferrerId)]
public Affiliate Referrer { get; set; }
//People the current has referred
public virtual ICollection<Affiliate> Referred { get; set; } = new HashSet<Affiliate>();
}
Now I want to retrieve an Affiliate
along with its children, grandchildren, and great grandchildren. Or I want to retrieve an Affiliate
with its Referrer
, he’s referrer’s referrer, and his referrer’s referrer’s referrer n
levels up.
How can this be achieved in a single round-trip without involving stored-procedures?
- Can Linq Expressions be used for this?
- Is there a repo for that?
- Anything for EF6?
Issue Analytics
- State:
- Created 8 years ago
- Comments:18 (12 by maintainers)
Top Results From Across the Web
N1QL query with Self Referencing Hierarchy
In this article, I will discuss how traditional RDBMS handles hierarchical queries. The challenges that it has to deal with, and how this...
Read more >loading a full hierarchy from a self referencing table with ...
Can anyone help me out by giving me some code how to load the full table (e.g. into memory in an optimal scenario...
Read more >ToSelfHierarchyList
The ToSelfHierarchyList method extend your Entity Framework DbContext to let you easily include a self hierarchy relationship.
Read more >Define and query hierarchical data with Microsoft Dataverse
With Microsoft Dataverse, hierarchical data structures are supported by self-referential one-to-many (1:N) relationships of the related rows.
Read more >Modeling self-referencing associations with Hibernate
When you model a hierarchical data structure, you often have to use self-referencing associations. Both ends of these associations are of the same...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@alvaromongon this actually helped me, you just need to be careful not to create a loop in db. I have made these methods in my UnitOfWork:
I know this thread is old but it pointed me in the right direction. I’m just posting this in case it helps other people. I had an issue where I had an hierarchy of categories of products, for instance, Storage->Internal Storage->SSD, and I had to retrieve the entire hierarchy from a child. The examples posted here were recursive, which can take a performance hit. You can do it iteratively this way: