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?
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);