我有 2 个实体,它们是一对多相关的
public class Restaurant {
public int RestaurantId {get;set;}
public string Name {get;set;}
public List<Reservation> Reservations {get;set;}
...
}
Run Code Online (Sandbox Code Playgroud)
public class Reservation{
public int ReservationId {get;set;}
public int RestaurantId {get;set;}
public Restaurant Restaurant {get;set;}
}
Run Code Online (Sandbox Code Playgroud)
如果我尝试使用我的 api 预订餐厅
var restaurants = await _dbContext.Restaurants
.AsNoTracking()
.AsQueryable()
.Include(m => m.Reservations).ToListAsync();
.....
Run Code Online (Sandbox Code Playgroud)
我收到错误响应,因为对象包含彼此的引用。有相关帖子建议创建单独的模型 或添加NewtonsoftJson 配置
问题是我不想创建单独的模型,第二个建议没有帮助。有没有办法在没有循环关系的情况下加载数据?*
System.Text.Json.JsonException: 检测到不支持的可能的对象循环。这可能是由于循环或对象深度大于最大允许深度 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, …