I have this entities:
public Order
{
long Id;
.......
List<Items> Items = new List<Items>();
}
public Items
{
long Id;
......
Order Order;
}
And this is my query witn EF Core 7:
_context.Orders
.AsNotracking()
.Include(X => x.Items)
.FirstOrDefault(x => x.Id == 1);
I am using AsNotraking() because I would like to include only the related entities that I indicate with a direct inculde. Son in this case I wouldn't expect that the items would populate the Order property.
However, with this query I would expect to populate the Order property of the Items, because I am explicitly indicate that I want the property, with ThenInclude():
_context.Orders
.AsNotracking()
.Include(X => x.Items).ThenInclude(x => x.Factura)
.FirstOrDefault(x => x.Id == 1);
I have read this question: Avoid or control circular references in Entity Framework Core
But the solution here is to configure in ASP the serialization. But my case is different.
I have a gRPC service, using code first, https://learn.microsoft.com/en-us/aspnet/core/grpc/code-first?view=aspnetcore-7.0
So setting the serialization at ASP level is not the solution.
So my question is, how to solve the problem at EF Core level? Because I thought that with AsNoTracking() entity resolution is not used so I don't know why the order property is populate if I don't use an explicit query.
One solution it is to clear the property iterating all the items of the order, but i would like to avoid this solution, if it is possible with EF Core to solve the problem in some way.
Thanks.