Entity to json error - a circular reference was found while serializing an object of type

An error occurred while trying to convert the object of the object to a JSON string. I am using C # MVC4 with developing the first code for the code. These are seams because FKs and table relationships create this problem. What will be the workaround?

A circular reference was found while serializing an object of type System.Data.Entity.DynamicProxies.User

my code

User ma = db.user.First(x => x.u_id == id); return Json(ma, JsonRequestBehavior.AllowGet); 
+7
json c # entity-framework asp.net-mvc-4
source share
4 answers

This is because it is trying to load child objects and can create some kind of looping cycle that will never end (a => b, b => c, c => d, d => a)

You can disable it only at that particular moment, as indicated below. So dbcontext will not load child objects of clients if the 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); 
+16
source share

My problem is solved with this:

 //initialize model db testdbEntities dc = new testdbEntities(); //get employee details List<Employee1> lst = dc.Employee1.ToList(); //selecting the desired columns var 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 JSON return this.Json(subCategoryToReturn , JsonRequestBehavior.AllowGet); 
+3
source share

I had the same problem that I did, it passed only the right column to view. In 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); 

Loop detected an exception while serializing an object in JSON

0
source share

it tries to load child objects and can create some loop property that never ends.

also you use [ScriptIgnore], you will not serialize a public property or public field look at this:

  public class BookingHotel : IntBaseEntity { public string BookingName { get; set; } public string BookingReference { get; set; } public DateTime? CheckInDate { get; set; } public DateTime? CheckOutDate { get; set; } public int HotelId { get; set; } [ScriptIgnore] public Hotel Hotel { get; set; } } 
0
source share

All Articles