public class Unit
{
Public int Id { get; set; }
Public virtual ICollection<Element> Elements { get; set; }
}
public class Element
{
public int Id { get; set; }
public virtual Unit Unit { get; set; }
}
We use a API call to get all Element
s with related Unit
s.
Like this:
Context.Elements.Include(o => o.Unit);
Our expectation was that only Element
s would have a Unit
. However the Unit
s do also have Element
s
{
"Id": 1,
"Unit": {
"Id": 1,
"Elements":[...]
}
}
How do we exclude the Element
s from Unit.Elements
?
There is currently no solution for this.
As pointed out here there is a workaround available:
EF Core populate entity's navigation properties if related entities already loaded in the memory.
Line above will load all
Element
entities in the memory, so it will populateUnit.Elements
as well.EF Core 2.1 now supports lazy loading which probably could answer your question, but, as I mentioned above, in your case all
Elements
already loaded in the memory.Loading Related Data - Lazing Loading