我有2个实体与一对多相关
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预订餐厅
var restaurants = await _dbContext.Restaurants
.AsNoTracking()
.AsQueryable()
.Include(m => m.Reservations).ToListAsync();
.....
我收到错误消息,因为对象包含彼此的引用。有相关的文章建议创建单独的模型 或添加NewtonsoftJson配置
问题是我不想创建单独的模型,第二个建议没有帮助。有没有没有循环关系的加载数据的方法吗?*
System.Text.Json.JsonException:检测到不支持的可能的对象循环。这可能是由于循环造成的,或者是对象深度大于System.Text.Json.ThrowHelper.ThrowInvalidOperationException_SerializerCycleDetected(Int32 maxDepth)处System.Text.Json.Json.JsonSerializer.Write(Utf8JsonWriter writer)处的最大深度。 ,Int32 originalWriterDepth,Int32 flushThreshold,JsonSerializerOptions选项,WriteStack和状态)位于System.Text.Json.JsonSerializer.WriteAsyncCore(Stream utf8Json,对象值,类型inputType,JsonSerializerOptions选项,CancellationToken cancelTokens)在Microsoft.AspNetFormat.System。 Microsoft.AspNetCore.Mvc上的WriteResponseBodyAsync(OutputFormatterWriteContext上下文,对selectedEncoding进行编码)。
*