Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A circular reference was detected while serializing an object of type 'System.Data.Entity.DynamicProxies

I have model contain class order in this action i create object from class order and fill data for it then serialize my object but throw exception

A circular reference was detected while serializing an object of type 'System.Data.Entity.DynamicProxies.Order_5247E51E921C7E989617CBA5719815085C1A2851AB5D00E28EC0BE368D8ECD38'.

i use mvc 4 and linq

my controller action

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

class order

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

how fix this problem ..

like image 698
Asmaa Mohamed Avatar asked Aug 31 '25 22:08

Asmaa Mohamed


1 Answers

Somewhere in the many properties on the Order class there is found an object which itself references that same Order object, potentially one more more levels deep in the object graph. This is likely happening in the virtual navigation properties. To simplify, imagine an Order and a 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; }
}

How would this be serialized? In order to serialize an Order you would also need to serialize its Customer property, and in order to serialize that property you would also need to serialize its Orders property, and in order to serialize those you would also need to serialize their Customer properties, and so on in an infinite recursion.

You're going to have to map out your object graph a little more carefully and make use of some attributes to tell the serializer what to ignore. In the simplified example above, I might ignore the Orders property since I really only care about the aggregate root Order and its Customer:

[JsonIgnore]
public virtual ICollection<Order> Orders { get; set; }

Depending on the libraries you're using there could be a variety of serialization attributes from which to choose. I believe JsonIgnore is part of the JSON.NET library. But whatever you use, the main point here is to explicitly tell the serialization what to include/exclude when serializing an object so as to avoid an infinite recursion.

like image 127
David Avatar answered Sep 03 '25 13:09

David