json.net;序列化实体框架对象(循环引用错误)

json.net; serialize entity framework object (circular reference error)(json.net;序列化实体框架对象(循环引用错误))
本文介绍了json.net;序列化实体框架对象(循环引用错误)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个实体框架实体,我想将其序列化为 json 对象.我环顾四周,发现 json.net (http://james.newtonking.com/projects/json-net.aspx) 应该能够使用开箱即用"的循环引用序列化对象.所以我尝试使用

I have an entity framework entity that i want to serialize as a json object. I looked around and found out that json.net (http://james.newtonking.com/projects/json-net.aspx) should be able to serialize objects with circular references "out of the box". So i tried using

string json = JsonConvert.SerializeObject(/* my ef entity */);

但我仍然遇到同样的错误.问题可能是我需要使用 ReferenceLoopHandling.IgnoreContractResolver,但我不确定如何使用它们.任何帮助深表感谢!谢谢

But im still getting the same error. The problem might be that i need to use ReferenceLoopHandling.Ignore and a ContractResolver, but im not sure how to use them. Any help is much appreciated! Thanks

推荐答案

我的解决方案是简单地删除子实体上的父引用.

My solution was to simply remove the parent reference on my child entities.

所以在我的模型中,我选择了关系并将父引用更改为内部而不是公共.

So in my model, I selected the relationship and changed the Parent reference to be Internal rather than Public.

可能不是所有人的理想解决方案,但对我有用.

May not be an ideal solution for all, but worked for me.

这篇关于json.net;序列化实体框架对象(循环引用错误)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)