-2

I've reviewed many solutions for entity framework Include() and can't find a solution that is working. When I use context.A.Include(x => x.B); it contains infinite references of the parent and child objects. I want to disable this. In other words, A -> B and B -> A and A -> B, etc etc. It's causing timeouts and performance issues. How do I avoid this? Using .NET Core 3.1.

Kota
  • 157
  • 7
  • You need it for serialization? – Genusatplay May 29 '21 at 21:13
  • I'm using an MVC setup to retrieve a table and all of it's foreign key references for user management in a site. But the data returning from the API should only have a parent and child object. Not all of the additional parent/child references being nested infinitely. – Kota May 29 '21 at 21:17
  • 1
    Check this [Entity Framework Infinite Self Referencing Entity](https://stackoverflow.com/a/54623498/5446495) – Genusatplay May 29 '21 at 21:20
  • Someone should venmo you $$$$$$. Thanks! ♥ – Kota May 29 '21 at 21:31
  • Just for reference for anyone else stuck on this: Package - Microsoft.AspNetCore.Mvc.NewtonsoftJson Dotnet version - 3.1 Code in Startup - services.AddMvc().AddNewtonsoftJson(options => options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore); – Kota May 29 '21 at 21:32
  • Another link https://stackoverflow.com/a/58517316/5446495 – Genusatplay May 29 '21 at 21:39

1 Answers1

0
public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc()
        .AddJsonOptions(
            options => options.SerializerSettings.ReferenceLoopHandling = 
              Newtonsoft.Json.ReferenceLoopHandling.Ignore
        );
}
Nima Talebi
  • 797
  • 1
  • 7
  • 21