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

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


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);


My problem is solved by using this :

//initialize model dbtestdbEntities dc = new testdbEntities();//get employee details List<Employee1> lst = dc.Employee1.ToList(); //selecting the desired columnsvar subCategoryToReturn = lst.Select(S => new {    Employee_Id = S.Employee_Id,    First_Name = S.First_Name,    Last_Name = S.Last_Name,    Manager_Id = S.Manager_Id});//returning JSONreturn this.Json(subCategoryToReturn , JsonRequestBehavior.AllowGet);


I was having the same issue, what I have done is have passed only needed column to view , In my my case. only 2.

List<SubCategory> lstSubCategory = GetSubCateroy() // list from repo var subCategoryToReturn = lstSubCategory.Select(S => new { Id  = S.Id, Name = S.Name }); return this.Json(subCategoryToReturn , JsonRequestBehavior.AllowGet);

Circular reference detected exception while serializing object to JSON