c# - A circular reference was detected while serializing an object of type 'System.Data.Entity.DynamicProxies.ProjectModel_ -
firstly using database first entity framework model classes auto generated given 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 controller code return json result getting customer information. need customerinfo , addresses , customersites 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 returning serializations error. have checked few posts have same situation have below 2 lines navigation properties not loaded.
db.configuration.lazyloadingenabled = false; db.configuration.proxycreationenabled = false;
when executing each line address customersites customer in last, able see after getting addresses virtual property customer did not load , customersites virtual property customer did not load when run after customer code able see virtual properties automatically loaded , serialization error coming.
when check other posts suggesting use model or retrieve properties required having many properties in customer cannot follow method.
can please me. have deadline coding.
Comments
Post a Comment