简体   繁体   English

实体到 json 错误 - 序列化类型对象时检测到循环引用

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

Following error occurred when trying to convert entity object into JSON String.尝试将实体对象转换为 JSON 字符串时发生以下错误。 I'm using C# MVC4 with code first DB designing.我将 C# MVC4 与代码优先数据库设计一起使用。 It seams its because FKs and relationships between tables create this issue.它接缝是因为 FK 和表之间的关系造成了这个问题。 What will be the workaround ?解决方法是什么?

A circular reference was detected while serializing an object of type System.Data.Entity.DynamicProxies.User序列化 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)这是因为它正在尝试加载子对象,并且它可能会创建一些永远不会结束的循环(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您只能在特定时刻将其关闭,如下所示。因此,除非在您的对象上调用 Include 方法,否则 dbcontext 不会加载客户子对象

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

I was having the same issue, what I have done is have passed only needed column to view , In my my case.我遇到了同样的问题,我所做的是只通过了需要的列来查看,就我而言。 only 2.只有 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将对象序列化为 JSON 时循环引用检测到异常

Why not create a class to hold the query contents?为什么不创建一个类来保存查询内容? That worked for me那对我有用

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 :你也使用 [ScriptIgnore] ,不会序列化公共属性或公共字段,看看这个:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Json在序列化类型的对象时检测到循环引用 - Json A circular reference was detected while serializing an object of type 序列化类型为object的对象时检测到循环引用 - A circular reference was detected while serializing an object of type 序列化一个类型的对象时检测到循环引用 - Circular reference was detected while serializing an object of type 序列化类型错误的对象时检测到循环引用? - A circular reference was detected while serializing an object of type error? JSON.stringify引发错误“序列化类型为&#39;System.Reflection.RuntimeModule&#39;的对象时检测到循环引用”。 ” - JSON.stringify throws error “A circular reference was detected while serializing an object of type 'System.Reflection.RuntimeModule'. ” 序列化“System.Data.Entity.DynamicProxies”类型的对象时检测到循环引用 - A circular reference was detected while serializing an object of type 'System.Data.Entity.DynamicProxies 序列化类型为“System.Data.Entity.DynamicProxies.Item”的对象时检测到循环引用 - A circular reference was detected while serializing an object of type 'System.Data.Entity.DynamicProxies.Item 循环引用在将对象序列化为JSON时检测到异常 - Circular reference detected exception while serializing object to JSON 序列化类型的对象时检测到循环引用...如何解决? - A circular reference was detected while serializing an object of type… How to solve this? 在序列化“住宅建筑”类型的对象时检测到循环参考 - A circular reference was detected while serializing an object of type Residential Building
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM