项目场景:
学习Furion+Asp.net Core
问题描述
进行多对多查询时出现的问题:
System.Text.Json.JsonException: A possible object cycle was detected. This can either be due to a cycle or if the object depth is larger than the maximum allowed depth of 32. Consider using ReferenceHandler.Preserve on JsonSerializerOptions to support cycles. Path:
系统文本Json。JsonException:检测到可能的对象循环。这可能是由于循环造成的,或者如果对象深度大于允许的最大深度32。考虑使用ReferenceHandler。在JsonSerializerOptions上保留以支持循环。
[HttpGet("多对多查询")]
public IIncludableQueryable<Person,ICollection<Post>> GetDDD()
{
//仓储
var repository = Db.GetRepository<Person>();
var person = repository.Include(u => u.Posts);
return person;
}
原因分析:
ef 查询关联加载引起的
解决方案:
Startup中添加服务,忽略循环引用
//一对一、一对多、多对多查询
services.AddControllers().AddNewtonsoftJson(option=>
//忽略循环引用
option.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
本文转载于:https://blog.csdn.net/weixin_44214243/article/details/106879549