-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Comments
Can you write a SQL to load all data in single query? |
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 |
@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. |
@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 |
Thanks @sdanyliv ! 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 |
Putting this on the backlog to consider implementing in the future. @lfwinster Two workarounds for now are:
|
Thanks @ajcvickers , I have considered taking the first work around until this is implemented. |
@ajcvickers , when will this be available? |
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 |
Related to #365 |
Note from triage: we do not plan to add direct support for this. |
I have table like this
ModelA
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.
The text was updated successfully, but these errors were encountered: