序列化类型 System.Data.Entity.DynamicProxies 的对象时检测到循环引用
生活随笔
收集整理的這篇文章主要介紹了
序列化类型 System.Data.Entity.DynamicProxies 的对象时检测到循环引用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?
??????? 學習 EF Code First+MVC 時遇到了在請求JsonResult時出現 序列化類型 System.Data.Entity.DynamicProxies 的對象時檢測到循環引用 的異常,原因是實體之間有關聯關系,在序列化這些相關的
數據對象的時候,產生無限循環引用的現象。
?????? Example:
public class CollegeInfoModel{public Guid CollegeId { get; set; }[Display(Name = "學院")]public string CollegeName { get; set; }[JsonIgnore]public virtual List<DepartmentInfoModel> Departments { get; set; }}public class DepartmentInfoModel{public Guid DepartmentId { get; set; }public string DepartmentName { get; set; }public virtual CollegeInfoModel CollegeInfoObj { get; set; }[JsonIgnore]public virtual List<ClassInfoModel> Classinfos { get; set; }}CollegeInfoModel中有List<DepartmentInfoModel>,DepartmentInfoModel中有CollegeInfoModel。
?
1.最簡單的解決方案:獲取需要的字段
from c in stuCtx.CollegeInfoes where c.CollegeId == collegeId select new { c.CollegeId, c.CollegeName };2.可以嘗試刪除所有的導航屬性的virtual關鍵字禁用延遲加載和創建代理,然后使用預先加載,而不是顯式地加載所需的對象圖
3.沒有必要刪除virtual關鍵字導航性能(這將使延遲加載的模型完全不可能)。這足以創建代理(以及禁用延遲加載)禁用代理擾亂序列一樣:
Context.Configuration.ProxyCreationEnabled = false;以上3種方案可以看? Xia.CJ 的博客。 參考
?
我的解決方案是自定義JsonResult來實現。
轉載于:https://www.cnblogs.com/guolihao/p/3214139.html
總結
以上是生活随笔為你收集整理的序列化类型 System.Data.Entity.DynamicProxies 的对象时检测到循环引用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Hbase数据模型 列族
- 下一篇: Entity Framework如何得到