[ACCEPTED]-Avoid or control circular references in Entity Framework Core-entity-framework-core
Assuming you are using the latest and greatest 12 ASP .NET Core 3.1 with System.Text.Json
, to handle reference 11 loops you will need to switch "back" to 10 Newtonsoft.Json
(though it worth mentioning that System.Text.Json
should 9 be faster. Also support for reference loops 8 handling is coming, as @Eric J. wrote in comments):
services.AddControllers()
.AddNewtonsoftJson(x => x.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore)
As 7 for EF creating reference loops - it is 6 called relationship fixup
and you can't do a lot about it 5 (see this answer). AsNoTracking
can help a little bit (but 4 not in case of Include
). My personal approach is 3 to return DTO's from endpoints and not entites 2 directly.
UPD
In .NET 5.0 ReferenceHandler
is introduced, so 1 next should do the trick:
services.AddControllersWithViews()
.AddJsonOptions(options =>
options.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.Preserve)
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.