How to ignore ThenInclude's related data?

c# entity-framework-core

Question

I'm developing an ASP.NET Core Web API with EF Core 2.0.

I have the following code:

return _context.Pessoas
    .Include(x => x.ListasXPessoas).ThenInclude(l => l.Lista)
    .Take(100);

Pessoas and Listas have a many-to-many relationship, ListasXPessoas is the join table.

I don't want to return the related data of the Listas entity, as it cycles back to Pessoas. It is defeating the purpose of the Take(100). Is there a way to turn off this behavior, or is executing a SQL command the only way?

1
3
2/12/2018 5:16:11 PM

Popular Answer

The issue isn't with the includes, I don't think recursive references effect performance unless you loop over them. The issue is that when you serialize to json you are ending up in endless loop.

Tell json formatters to ignore recursive references:

services.AddMvcCore()
        .AddJsonFormatters(opt => opt.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore);
0
2/13/2018 1:06:02 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow