Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support loading recursive relationships to any depth #14144

Closed
ghost opened this issue Dec 11, 2018 · 12 comments
Closed

Support loading recursive relationships to any depth #14144

ghost opened this issue Dec 11, 2018 · 12 comments
Labels
closed-out-of-scope This is not something that will be fixed/implemented and the issue is closed. customer-reported

Comments

@ghost
Copy link

ghost commented Dec 11, 2018

I have table like this

ModelA

ID ParentID
1 NULL
2 1
3 2
4 3
5 4
public partial class Model A
{
    public int ID {get;set;}
    public ParentID? {get;set}
    [ForeignKey("ID")]
    public ModelA Parent {get;set;}
}

I can get Parent navigation property populated by doing

var obj = _context.ModelA.Include(x=>x.Parent).Single(x=>x.ID == 5);

but the obj.Parent.Parent will be null unless I do

var obj = _context.ModelA.Include(x=>x.Parent).ThenInclude(x=>x.Parent))Single(x=>x.ID == 5);

the issue is for any entity, there could be X amount of nested navigation property until the actual ParentID is null. I cannot hard code the ThenInclude for derived type.

var obj = _context.ModelA.Include(x=>x.Parent).ThenInclude(x=>x.Parent))...(N amount of ThenInclude)...Single(x=>x.ID == N);

Also I do not wish to use lazy loading to solve this issue.

@smitpatel
Copy link
Contributor

Can you write a SQL to load all data in single query?

@ghost
Copy link
Author

ghost commented Dec 11, 2018

Yes I can, but in my case i need to figure out, when given an ID, all its ancestors and navigate through the nav properties

@smitpatel
Copy link
Contributor

@lfwinster - I am asking you to provide a SQL. If it cannot be expression in SQL then it cannot be done in LINQ either. The issue in linq/SQL both you face is, you don't know how many times you have to do join. If you have a clever way to do it in SQL, we can think about how to support it in linq.

@sdanyliv
Copy link

sdanyliv commented Dec 12, 2018

@smitpatel, it is easily doeable by recursive common table expressions (CTE), which are supported by many database engines.

with hierarchy_up (id, parent_id)
as
(
    select id, parent_id from model_a
    where id = 5
    union all
    select parent.id, parent.parent_id 
    from hierarchy_up h
    inner join model_a parent on parent.id = h.parent_id
)
select * from hierarchy_up

@ghost
Copy link
Author

ghost commented Dec 12, 2018

Thanks @sdanyliv !
your recursive CTE does the job. I assume for hierarchy questions, there will always be recursion/loop involved.

does that mean Lazy loading is not translating the LINQ into the recursive CTE that @sdanyliv mentioned but rather into multiple sql queries based on how the navigation properties are being consumed in the code?

for example, if i am only accessing the parent vs im accessing all the way to the original ancestor, LINQ will translate into different SQL

@ajcvickers
Copy link
Contributor

Putting this on the backlog to consider implementing in the future.

@lfwinster Two workarounds for now are:

  • Load all entities of type ModelA into the context; all parent/children navigation properties will be fixed up.
  • Decide an acceptable depth to go to and add Include/ThenInclude/ThenInclude/etc. up to that depth.

@ajcvickers ajcvickers added this to the Backlog milestone Dec 15, 2018
@ghost
Copy link
Author

ghost commented Dec 18, 2018

Putting this on the backlog to consider implementing in the future.

@lfwinster Two workarounds for now are:

  • Load all entities of type ModelA into the context; all parent/children navigation properties will be fixed up.
  • Decide an acceptable depth to go to and add Include/ThenInclude/ThenInclude/etc. up to that depth.

Thanks @ajcvickers , I have considered taking the first work around until this is implemented.

@ajcvickers ajcvickers changed the title Eager loading for one-to-one relationship table that has navigation property to the same table Support loading recursive relationships to any depth Aug 24, 2021
@tnlthanzeel
Copy link

@ajcvickers , when will this be available?

@ajcvickers
Copy link
Contributor

This issue is in the Backlog milestone. This means that it is not planned for the next release (EF Core 6.0). We will re-assess the backlog following the this release and consider this item at that time. However, keep in mind that there are many other high priority features with which it will be competing for resources. Please vote (👍) for this issue if it is important to you.

@tnlthanzeel
Copy link

This issue is in the Backlog milestone. This means that it is not planned for the next release (EF Core 6.0). We will re-assess the backlog following the this release and consider this item at that time. However, keep in mind that there are many other high priority features with which it will be competing for resources. Please vote (👍) for this issue if it is important to you.

noted! thanks for the reply

@AndriySvyryd
Copy link
Member

Related to #365

@ajcvickers
Copy link
Contributor

Note from triage: we do not plan to add direct support for this.

@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 26, 2022
@ajcvickers ajcvickers added closed-out-of-scope This is not something that will be fixed/implemented and the issue is closed. and removed type-enhancement propose-close area-query labels Oct 26, 2022
@ajcvickers ajcvickers removed this from the Backlog milestone Oct 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-out-of-scope This is not something that will be fixed/implemented and the issue is closed. customer-reported
Projects
None yet
Development

No branches or pull requests

5 participants