0

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.

Álvaro García
  • 18,114
  • 30
  • 102
  • 193
  • 1
    The answer has another option specified - _"My personal approach is to return DTO's from endpoints and not entities directly."_ – Guru Stron Aug 02 '23 at 09:54
  • _" I don't know why the order property is populate if I don't use an explicit query."_ - but you are explicitly/eager loading the data with `Include`/`ThenInclude`, `AsNotracking` has no effect on that. – Guru Stron Aug 02 '23 at 09:56
  • Perhaps this question is not directly related to EF but rather to gRPC serialization. Specifically, we can differentiate between two distinct parts: the first involves accessing the database to instantiate models, while the second involves transforming these models into gRPC data. In my opinion, both processes are independent, and you are inquiring about the second one, not the entire process. Do you believe it's possible to isolate the issue to the second part? – dani herrera Aug 02 '23 at 10:18
  • Yes, with inlcude I expect to populate the collection of items in the order, but not populate the Order property of the included items. However, with Include().ThenInclude(x => x.Order) I expect to populate the order property because I explicitly tells with the includes. – Álvaro García Aug 02 '23 at 10:21
  • The solution of returning DTOs instead of entities, I am not sure to understand it. Because the classes that I expose in the original question are DTOs, they don't have behaviour. they only have a bidirectional relationship. – Álvaro García Aug 02 '23 at 10:22
  • @daniherrera Yes, from some point of view it is possible to considerate a problem more than the serialization than the result of the query with EF. But from a different point of view i think it is a bit confuse or not intuitive that EF populate the Order property when I don't set to do it with an explicit inlcude. But yes, I would prefer to solve the problem at EF level, but it I can't do it, I will try in the serialization side. – Álvaro García Aug 02 '23 at 10:28
  • `but not populate the Order property of the included items.` - this does not work this way. If the data is present (via tracking or eager loading) it will be fixed up, that's it, nothing can be done here. _"The solution of returning DTOs instead of entities, I am not sure to understand it"_ - I mean - create special classes, not the ones used by EF and map them manually. – Guru Stron Aug 02 '23 at 10:40
  • Your question is a bit confusing. Could you please clarify your requirement explicitly? For instance, you may say, "I want EF not to load the `Order` navigation property for each `Item`." – dani herrera Aug 02 '23 at 10:42

0 Answers0