检测到不支持的.Net Core 3.0可能的对象周期 [英] .Net Core 3.0 possible object cycle was detected which is not supported
问题描述
我有2个相互关联的实体
I have 2 entities that are related as one to many
public class Restaurant {
public int RestaurantId {get;set;}
public string Name {get;set;}
public List<Reservation> Reservations {get;set;}
...
}
public class Reservation{
public int ReservationId {get;set;}
public int RestaurantId {get;set;}
public Restaurant Restaurant {get;set;}
}
如果我尝试使用自己的api获取预订的餐厅
If I try to get restaurants with reservations using my api
var restaurants = await _dbContext.Restaurants
.AsNoTracking()
.AsQueryable()
.Include(m => m.Reservations).ToListAsync();
.....
由于对象包含彼此的引用,因此我收到错误消息. 有相关的帖子建议创建单独的模型 或添加 NewtonsoftJson配置
I receive error in response, because objects contain references to each other. There are related posts that recommend to create separate model or add NewtonsoftJson configuration
问题是我不想创建单独的模型,第二个建议也无济于事. 有没有没有循环关系的加载数据的方法吗? *
Problem is that I do not want to create separate model and 2nd suggestion didn't help. Is there any way to load data without cycled relationship ? *
System.Text.Json.JsonException:检测到可能的对象周期 不支持.这可能是由于周期造成的,也可能是由于 对象深度大于最大允许深度32. System.Text.Json.ThrowHelper.ThrowInvalidOperationException_SerializerCycleDetected(Int32 maxDepth)位于System.Text.Json.JsonSerializer.Write(Utf8JsonWriter writer,Int32 originalWriterDepth,Int32 flushThreshold, JsonSerializerOptions选项,WriteStack&州) System.Text.Json.JsonSerializer.WriteAsyncCore(Stream utf8Json,Object 值,类型inputType,JsonSerializerOptions选项, 的CancellationToken cancelToken() Microsoft.AspNetCore.Mvc.Formatters.SystemTextJsonOutputFormatter.WriteResponseBodyAsync(OutputFormatterWriteContext 上下文,编码selected(Encoding) Microsoft.AspNetCore.Mvc.Formatters.SystemTextJsonOutputFormatter.WriteResponseBodyAsync(OutputFormatterWriteContext 上下文,编码selected(Encoding) Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited | 29_0 [TFilter,TFilterAsync](ResourceInvoker 调用程序,任务lastTask,状态下一个,作用域范围,对象状态,布尔值 isCompleted) Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResultExecutedContextSealed 上下文) Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.ResultNext [TFilter,TFilterAsync](状态 接下来,Scope&范围,对象与对象状态,布尔值和isCompleted) Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeResultFilters()
System.Text.Json.JsonException: A possible object cycle was detected which is not supported. This can either be due to a cycle or if the object depth is larger than the maximum allowed depth of 32. at System.Text.Json.ThrowHelper.ThrowInvalidOperationException_SerializerCycleDetected(Int32 maxDepth) at System.Text.Json.JsonSerializer.Write(Utf8JsonWriter writer, Int32 originalWriterDepth, Int32 flushThreshold, JsonSerializerOptions options, WriteStack& state) at System.Text.Json.JsonSerializer.WriteAsyncCore(Stream utf8Json, Object value, Type inputType, JsonSerializerOptions options, CancellationToken cancellationToken) at Microsoft.AspNetCore.Mvc.Formatters.SystemTextJsonOutputFormatter.WriteResponseBodyAsync(OutputFormatterWriteContext context, Encoding selectedEncoding) at Microsoft.AspNetCore.Mvc.Formatters.SystemTextJsonOutputFormatter.WriteResponseBodyAsync(OutputFormatterWriteContext context, Encoding selectedEncoding) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|29_0[TFilter,TFilterAsync](ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResultExecutedContextSealed context) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.ResultNext[TFilter,TFilterAsync](State& next, Scope& scope, Object& state, Boolean& isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeResultFilters()
*
推荐答案
I have tried your code in a new project and the second way seems to work well after installing the package Microsoft.AspNetCore.Mvc.NewtonsoftJson firstly for 3.0
services.AddControllersWithViews()
.AddNewtonsoftJson(options =>
options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
);
尝试一个新项目并比较差异.
Try with a new project and compare the differences.
这篇关于检测到不支持的.Net Core 3.0可能的对象周期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!