EF+NewtonSoft报循环引用错

netspider0533 2013-08-21 12:36:58

JsonSerializerSettings setting = new JsonSerializerSettings()
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
PreserveReferencesHandling = PreserveReferencesHandling.Objects//,
//NullValueHandling = NullValueHandling.Ignore
};
PBInfo p = new PBInfoBLL().Get(5000);

string s = JsonConvert.SerializeObject(p, Formatting.None, setting);


上面的代码运行到JsonConvert.SerializeObject就死住了,再也不动了,好象一直在序列化循环对象,
如果只是:JsonConvert.SerializeObject(p),报循环引用错
...全文
136 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
netspider0533 2013-08-21
  • 打赏
  • 举报
回复
EF LazyLoading肯定是都嵌套在一起,比较大,有什么好的解决办法吗?
宝_爸 2013-08-21
  • 打赏
  • 举报
回复
引用 3 楼 netspider0533 的回复:
p里面是有loop,但已经加了ReferenceLoopHandling.Ignore不就应该中止了?
你的loop是不是圈子比较大啊,不是parent reference child 并且child reference parent. 这里是document中的解释: Error: By default Json.NET will error if a reference loop is encountered (otherwise the serializer will get into an infinite loop). Ignore: Json.NET will ignore objects in reference loops and not serialize them. The first time an object is encountered it will be serialized as usual but if the object is encountered as a child object of itself the serializer will skip serializing it. Serialize: This option forces Json.NET to serialize objects in reference loops. This is useful if objects are nested but not indefinitely.
netspider0533 2013-08-21
  • 打赏
  • 举报
回复
p里面是有loop,但已经加了ReferenceLoopHandling.Ignore不就应该中止了?
宝_爸 2013-08-21
  • 打赏
  • 举报
回复
p里面的引用有loop了吧。
myhope88 2013-08-21
  • 打赏
  • 举报
回复
没遇到过,帮顶下
winner2050 2013-08-21
  • 打赏
  • 举报
回复
加上 [DataContract(IsReference = true)] 就行了。

62,041

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧