序列化“System.Data.Entity.DynamicProxies.ProjectModel _

时间:2015-06-13 11:16:02

标签: c# json entity-framework

首先,我使用的是数据库第一实体框架,因此我的所有模型类都是自动生成的 鉴于课程:

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

和我的控制器代码返回json结果以获取客户信息。我需要分别获得customerinfo,地址和客户。

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

因此json返回序列化错误。我检查了几个帖子,他们有相同的情况所以我有以下两行,以便不会加载导航属性。

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

当执行每一行获取地址然后客户端然后是最后一位客户时,我能够看到在获取虚拟财产客户未加载的地址之后,对于客户而言,虚拟财产客户也未加载但当我在客户代码之后运行时,我能够看到虚拟属性被自动加载,因此序列化错误即将到来。

当我检查其他帖子时,他们建议使用其他模型或仅检索所需的属性,但由于我在客户中有许多属性,因此我无法遵循此方法。

有人可以帮助我。我有一些编码的截止日期。

0 个答案:

没有答案