与EF4循环引用 [英] Circular reference with EF4
问题描述
我想带着孩子到jQuery的JSON AJAX功能返回实体框架4的对象,但我得到一个循环引用错误 - 总之我的方法是这样的
I am trying to return an Entity Framework 4 object with children to an jQuery JSON AJAX function but I get a circular reference error - in short my method looks like this
[WebMethod]
public static JSONObject Get()
{
WebHelper.JSONObject lJSONObject = new WebHelper.JSONObject();
lJSONObject.Object = Repository.Parent.Include("Child.Child").FirstOrDefault();
return lJSONObject;
}
如果我不包括儿童的功能工作正常,但有孩子的循环引用发生。任何想法我能做些什么来解决这个问题?
if I do not include children the functions works fine, but with children the circular reference occurs. Any ideas what I can do to fix this?
推荐答案
你必须返回一个JSONObject?如果没有,你可以尝试使用 Json.NET ,这将妥善处理循环引用:
Do you have to return a JSONObject? If not, you can try using Json.NET, which will handle circular references properly:
var settings = new JsonSerializerSettings
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
};
JsonConvert.SerializeObject(object, Formatting.Indented, settings);
我的猜测是,的JSONObject
很简单,就是将序列化实体,并把它的响应流,这是很简单的手工做的包装。
My guess is that the JsonObject
is simply a wrapper that will serialize the entity and put it on the response stream, which is simple enough to do manually.
这篇关于与EF4循环引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!