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

First, I am using the underlying database entity framework, so all my model classes are automatically generated Given the classes:

public partial class Customer
{
    public Customer()
    {
        this.CustomerSites = new HashSet<CustomerSite>();
        this.Addresses = new HashSet<Address>();
    }

    public int CustomerId { get; set; }
    public string CustomerName { get; set; }
    public string Notes { get; set; }
    public Nullable<int> Company_ID { get; set; }
    public string UserName { get; set; }
    public string Phone { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }
    public string Fax { get; set; }
    public string Mobile { get; set; }
    public string Address { get; set; }
    public string Status { get; set; }
    public string UnitNo { get; set; }
    public string ContactPerson { get; set; }
    public string PIC { get; set; }
    public string DisplayName { get; set; }
    public string Website { get; set; }
    public Nullable<int> FK_CurrencyID { get; set; }
    public Nullable<int> FK_PaymentTermsID { get; set; }
    public Nullable<bool> AllowPortal { get; set; }
    public Nullable<bool> IsInvited { get; set; }
    public Nullable<bool> DidAccess { get; set; }
    public Nullable<bool> IsCustomer { get; set; }
    public Nullable<bool> IsSupplier { get; set; }
    public Nullable<bool> IsActive { get; set; }

    public virtual Company Company { get; set; }
    public virtual ICollection<CustomerSite> CustomerSites { get; set; }
    public virtual ICollection<Address> Addresses { get; set; }
}

public partial class Address
{
    public int AddressId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Phone { get; set; }
    public string Mobile { get; set; }
    public string Email { get; set; }
    public string Fax { get; set; }
    public string Address1 { get; set; }
    public string Street { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string PostalCode { get; set; }
    public Nullable<int> FK_CountryId { get; set; }
    public int FK_CustomerId { get; set; }
    public Nullable<int> FK_AddressTypeId { get; set; }

    public virtual Country Country { get; set; }
    public virtual Customer Customer { get; set; }
}

public partial class CustomerSite
{
    public int SiteId { get; set; }
    public string SiteName { get; set; }
    public string Address { get; set; }
    public double Latitude { get; set; }
    public double Longitude { get; set; }
    public string Description { get; set; }
    public int FK_CustomerID { get; set; }

    public virtual Customer Customer { get; set; }
}

      

and my controller code to return json result for client information. I need to get customerinfo, addresses and customers separately.

db.Configuration.LazyLoadingEnabled = false;
db.Configuration.ProxyCreationEnabled = false;
var billingaddress = db.Addresses.Where(x => x.FK_CustomerId == ID && x.FK_AddressTypeId.Value == (int)AddressTypes.Billing).FirstOrDefault();
var shippingaddress = db.Addresses.Where(x => x.FK_CustomerId == ID && x.FK_AddressTypeId.Value == (int)AddressTypes.Shipping).FirstOrDefault();
List<CustomerSite> customersites = db.CustomerSites.Where(x => x.FK_CustomerID == ID).ToList();
Customer customerinfo = db.Customers.Where(x => x.CustomerId == ID && x.IsActive == true).FirstOrDefault();
return Json(new { customerinfo = customerinfo == null ? new Customer() : customerinfo, billingaddress = billingaddress == null ? new Address() { FK_AddressTypeId = (int)AddressTypes.Billing, FK_CustomerId = 0 } : billingaddress, shippingaddress = shippingaddress == null ? new Address() { FK_AddressTypeId = (int)AddressTypes.Shipping, FK_CustomerId = 0 } : shippingaddress, customersites = customersites == null ? new List<CustomerSite>() : customersites },JsonRequestBehavior.AllowGet);

      

So json is returning serialization error. I have checked several posts where they have the same situation, so I have two lines so the navigation properties will not be loaded.

db.Configuration.LazyLoadingEnabled = false;
db.Configuration.ProxyCreationEnabled = false;

      

when it executes each line to get address and then clients and then client one last time, I was able to see that after getting addresses the virtual property client did not load and for clients also the virtual property client did not load but when I run after the client code , I was able to see that the virtual properties are automatically loaded and hence the serialization error occurs.

When I check other posts, they suggest using a different model, or retrieving only the properties I need, but since I have a lot of properties in the client, I cannot follow this method.

Can someone please help me. I have some deadline for coding.

+3


source to share





All Articles