A possible object cycle was detected. This can either be due to a cycle or if the object depth is l

本文介绍了在使用Furion+Asp.NetCore项目中遇到的多对多查询时的Json循环引用问题,通过在Startup中添加NewtonsoftJson选项来忽略循环引用,确保了查询的正常执行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

项目场景:

学习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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值