而序列化类型的对象时检测到循环引用 - 实体JSON错误 [英] Entity to json error - A circular reference was detected while serializing an object of type
问题描述
继试图实体对象转换成JSON字符串时发生错误。我使用C#MVC4与code首先DB设计。它接缝,因为其表之间FKS和关系创建这个问题。会有什么解决办法?
在序列化类型System.Data.Entity.DynamicProxies.User的对象时检测到循环引用
我的code是
用户MA = db.user.First(X => x.u_id == ID);
返回JSON(MA,JsonRequestBehavior.AllowGet);
它,因为它试图加载子对象,它可以创造一些圆环,将永远不会结束的(A => B,B => C,C => D,D => A)
您可以关闭它仅适用于特定的时刻,following.So的DbContext将不会加载客户子对象,除非包含方法被调用的对象上。
db.Configuration.ProxyCreationEnabled = FALSE;
用户MA = db.user.First(X => x.u_id == ID);
返回JSON(MA,JsonRequestBehavior.AllowGet);
Following error occurred when trying to convert entity object into JSON String. I'm using C# MVC4 with code first DB designing. It seams its because FKs and relationships between tables create this issue. What will be the workaround ?
A circular reference was detected while serializing an object of type System.Data.Entity.DynamicProxies.User
my code is
User ma = db.user.First(x => x.u_id == id);
return Json(ma, JsonRequestBehavior.AllowGet);
Its because it is trying to load child objects and it may be creating some circular loop that will never ending( a=>b, b=>c, c=>d, d=>a)
you can turn it off only for that particular moment as following.So dbcontext will not load customers child objects unless Include method is called on your object
db.Configuration.ProxyCreationEnabled = false;
User ma = db.user.First(x => x.u_id == id);
return Json(ma, JsonRequestBehavior.AllowGet);
这篇关于而序列化类型的对象时检测到循环引用 - 实体JSON错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!