Entity to json error - A circular reference was detected while serializing an object of type

.net asp.net-mvc-4 c# entity-framework json

Question

When trying to convert an entity object into a JSON string, the following issue was encountered. I'm designing my database utilizing code first in C# MVC4. It appears that this problem is caused by FKs and linkages between tables. What will the solution be?

While serializing an object of type System, a circular reference was found. Data.Entity. DynamicProxies.User

I'm using

  User ma = db.user.First(x => x.u_id == id);
  return Json(ma, JsonRequestBehavior.AllowGet);
1
12
1/29/2016 6:18:02 AM

Accepted Answer

Because it is attempting to load child objects, a never-ending circular loop (a=>b, b=>c, c=>d, d=>a) may be created.

You can do the following to turn it off just for that instant. Therefore, until the Include method is used on your object, dbcontext will not load customers' child objects.

  db.Configuration.ProxyCreationEnabled = false;
  User ma = db.user.First(x => x.u_id == id);
  return Json(ma, JsonRequestBehavior.AllowGet);
33
10/5/2014 7:29:12 PM

Popular Answer

ZZZ_tmp


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow