我的模型在此操作中包含类顺序我从类顺序创建对象并为其填充数据然后序列化我的对象但抛出异常
序列化“System.Data.Entity.DynamicProxies.Order_5247E51E921C7E989617CBA5719815085C1A2851AB5D00E28EC0BE368D8ECD38”类型的对象时检测到循环引用。
我使用mvc 4和linq
我的控制器动作
public ActionResult Posters(Order Obj)
{
Obj = new Order();
//fill data of object Obj
Session.Add("ObjOrder", Obj);
String serializedResult = new JavaScriptSerializer().Serialize((Order)Session["ObjOrder"]);
ViewBag.Order = serializedResult;
serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
serializedResult = serializer.Serialize(page_OrderBostrat);
ViewBag.BoxNum = serializedResult;
var con = new { ViewBag.BoxNum, ViewBag.Order };
page_OrderBostrat++;
return Json(con, JsonRequestBehavior.AllowGet);
}
班级订单
namespace MVCProject.Models
{
using System;
using System.Collections.Generic;
public partial class Order
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Order()
{
this.IllustrativeFiles = new HashSet<IllustrativeFile>();
this.Links = new HashSet<Link>();
this.LogoColors = new HashSet<LogoColor>();
this.LogoFiles = new HashSet<LogoFile>();
this.ModelFiles = new HashSet<ModelFile>();
this.Other = new HashSet<Other>();
this.TatbeqatModelsImages = new HashSet<TatbeqatModelsImage>();
this.ComputerPrograms = new HashSet<ComputerProgram>();
this.InfographicModels = new HashSet<InfographicModel>();
this.LogoModels = new HashSet<LogoModel>();
this.PaintModels = new HashSet<PaintModel>();
this.PosterModels = new HashSet<PosterModel>();
this.PropagandaModels = new HashSet<PropagandaModel>();
this.PublicationsModelsSizes = new HashSet<PublicationsModelsSize>();
this.ResponsiveDesigns = new HashSet<ResponsiveDesign>();
this.Structurals = new HashSet<Structural>();
}
public int Id { get; set; }
public string Name { get; set; }
public int TypeId { get; set; }
public string Details { get; set; }
public decimal Height { get; set; }
public decimal Width { get; set; }
public string MainColor { get; set; }
public string SecondColor { get; set; }
public string ThirdColor { get; set; }
public int PaintTypeId { get; set; }
public int Language { get; set; }
public string LanguageName { get; set; }
public int Pages { get; set; }
public int DesignType { get; set; }
public string FirstName { get; set; }
public string SecondName { get; set; }
public string Country { get; set; }
public string City { get; set; }
public string Email { get; set; }
public string Mobile { get; set; }
public string Skype { get; set; }
public string Comment { get; set; }
public System.DateTime DateAdd { get; set; }
public bool Active { get; set; }
public bool Seen { get; set; }
public string IpAddress { get; set; }
public string IpCountry { get; set; }
public Nullable<bool> IsResponsiveDesign { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<IllustrativeFile> IllustrativeFiles { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Link> Links { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<LogoColor> LogoColors { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<LogoFile> LogoFiles { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<ModelFile> ModelFiles { get; set; }
public virtual OrderType OrderType { get; set; }
public virtual PaintType PaintType { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Other> Other { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<TatbeqatModelsImage> TatbeqatModelsImages { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<ComputerProgram> ComputerPrograms { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<InfographicModel> InfographicModels { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<LogoModel> LogoModels { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<PaintModel> PaintModels { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<PosterModel> PosterModels { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<PropagandaModel> PropagandaModels { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<PublicationsModelsSize> PublicationsModelsSizes { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<ResponsiveDesign> ResponsiveDesigns { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Structural> Structurals { get; set; }
}
}
如何解决这个问题..
答案 0 :(得分:2)
在Order
类的许多属性中的某处,找到了一个对象,该对象本身引用了相同的Order
对象,可能在对象图中更深层次。这可能发生在virtual
导航属性中。为简化起见,想象一下Order
和Customer
:
class Order
{
// properties describing the order
// and the customer who placed the order...
public virtual Customer Customer { get; set; }
}
class Customer
{
// properties describing the customer
// and the customer's orders...
public virtual ICollection<Order> Orders { get; set; }
}
如何序列化?为了序列化Order
,您还需要序列化其Customer
属性,并且为了序列化该属性,您还需要序列化其Orders
属性,以便序列化那些你还需要序列化他们的Customer
属性,等等在无限递归中。
您将不得不更仔细地绘制对象图,并利用一些属性告诉序列化器要忽略什么。在上面的简化示例中,我可能会忽略Orders
属性,因为我真的只关心聚合根Order
及其Customer
:
[JsonIgnore]
public virtual ICollection<Order> Orders { get; set; }
根据您使用的库,可能有多种序列化属性可供选择。我相信JsonIgnore
是JSON.NET库的一部分。但无论你使用什么,这里的要点是明确告诉序列化在序列化对象时要包含/排除什么,以避免无限递归。
答案 1 :(得分:1)
Json解析器必须循环反序列化对象的每个属性。其中一个属性是包含对初始对象的引用的对象。
如果您的实体具有导航属性到其他实体并且可以从这些实体导航回来,那么您有一个循环引用。它将反序列化对象,导航到第二个对象并反序列化它,然后找到原始对象的导航并反序列化,依此类推。这是一个无限循环。
您需要告诉反序列化器使用注释忽略具有循环引用的字段 [JsonIgnore]