json.net;序列化框架的实体对象(循环引用错误) [英] json.net; serialize entity framework object (circular reference error)
问题描述
我有我想要序列作为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.Ignore
和 ContractResolver
,但林不知道如何使用它们。任何帮助深表AP preciated!谢谢
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;序列化框架的实体对象(循环引用错误)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!