13

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);
kuma DK
  • 1,812
  • 1
  • 18
  • 36

5 Answers5

39

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);
kuma DK
  • 1,812
  • 1
  • 18
  • 36
6

My problem is solved by using 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);
Syed Ali hassan
  • 596
  • 4
  • 16
Ashish
  • 61
  • 1
  • 1
  • If you're just trying to solve the circular reference problem, turning off lazy loading will likely solve the issue (`context.Configuration.EnableLazyLoading = false`). If you do this in your controller, it will be temporary and only apply to that method; or you can set it in your context constructor. – Daniel Jan 24 '20 at 17:35
2

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

Community
  • 1
  • 1
BJ Patel
  • 6,148
  • 11
  • 47
  • 81
2

Why not create a class to hold the query contents? That worked for me

0

it is trying to load child objects and it may be creating some circular loop property that will never end.

also you use [ScriptIgnore] , will not serialize the 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; }        
     }         
ABlue
  • 664
  • 6
  • 20