gpt4 book ai didi

c# - EF 4.1 - 代码优先 - JSON 循环引用序列化错误

转载 作者:IT老高 更新时间:2023-10-28 12:45:52 26 4
gpt4 key购买 nike

我收到一个循环引用序列化错误,但据我所知,我没有任何循环引用。我正在从数据库中检索一组订单并将它们作为 JSON 发送到客户端。所有代码如下所示。

这是错误:

Error

A circular reference was detected while serializing an object of type 'System.Data.Entity.DynamicProxies.Order_83CECF2AA4DE38232F9077D4B26941AB96BC61230419EA8AC42C9100E6072812'. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: A circular reference was detected while serializing an object of type 'System.Data.Entity.DynamicProxies.Order_83CECF2AA4DE38232F9077D4B26941AB96BC61230419EA8AC42C9100E6072812'.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

我的课如下:

订购

public class Order
{
[Key]
public int OrderId { get; set; }

public int PatientId { get; set; }
public virtual Patient Patient { get; set; }

public int CertificationPeriodId { get; set; }
public virtual CertificationPeriod CertificationPeriod { get; set; }

public int AgencyId { get; set; }
public virtual Agency Agency { get; set; }

public int PrimaryDiagnosisId { get; set; }
public virtual Diagnosis PrimaryDiagnosis { get; set; }

public int ApprovalStatusId { get; set; }
public virtual OrderApprovalStatus ApprovalStatus { get; set; }

public int ApproverId { get; set; }
public virtual User Approver { get; set; }

public int SubmitterId { get; set; }
public virtual User Submitter { get; set; }

public DateTime ApprovalDate { get; set; }

public DateTime SubmittedDate { get; set; }
public Boolean IsDeprecated { get; set; }
}

患者

public class Patient
{
[Key]
public int PatientId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string MiddleInitial { get; set; }
public bool IsMale;
public DateTime DateOfBirth { get; set; }

public int PatientAddressId { get; set; }
public Address PatientAddress { get; set; }

public bool IsDeprecated { get; set; }
}

认证期限

public class CertificationPeriod
{
[Key]
public int CertificationPeriodId { get; set; }
public DateTime startDate { get; set; }
public DateTime endDate { get; set; }
public bool isDeprecated { get; set; }
}

代理机构

public class Agency
{
[Key]
public int AgencyId { get; set; }
public string Name { get; set; }

public int PatientAddressId { get; set; }
public virtual Address Address { get; set; }
}

诊断

public class Diagnosis
{
[Key]
public int DiagnosisId { get; set; }
public string Icd9Code { get; set; }
public string Description { get; set; }
public DateTime DateOfDiagnosis { get; set; }
public string Onset { get; set; }
public string Details { get; set; }
}

OrderApprovalStatus

public class OrderApprovalStatus
{
[Key]
public int OrderApprovalStatusId { get; set; }
public string Status { get; set; }
}

用户

public class User
{
[Key]
public int UserId { get; set; }
public string Login { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string NPI { get; set; }
public string Email { get; set; }

}

NOTE: ADDRESS CLASS IS NEW ADDITION DURING EDIT

地址

public class Address
{
[Key]
public int AddressId { get; set; }
public string StreetAddress { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Zip { get; set; }
public string Phone { get; set; }
public string Title { get; set; }
public string Label { get; set; }
}

执行序列化的代码在这里:

订单 Controller 摘录

    public ActionResult GetAll()
{
return Json(ppEFContext.Orders, JsonRequestBehavior.AllowGet);
}

谢谢

最佳答案

您可以尝试从所有导航属性中删除 virtual 关键字以禁用延迟加载和代理创建,然后使用预先加载来显式加载所需的对象图:

public ActionResult GetAll()
{
return Json(ppEFContext.Orders
.Include(o => o.Patient)
.Include(o => o.Patient.PatientAddress)
.Include(o => o.CertificationPeriod)
.Include(o => o.Agency)
.Include(o => o.Agency.Address)
.Include(o => o.PrimaryDiagnosis)
.Include(o => o.ApprovalStatus)
.Include(o => o.Approver)
.Include(o => o.Submitter),
JsonRequestBehavior.AllowGet);
}

引用您的previous post看起来您的应用程序无论如何都不依赖延迟加载,因为您在那里引入了虚拟属性来延迟加载对象图,这可能导致现在的序列化问题。

编辑

没有必要从导航属性中删除 virtual 关键字(这将使模型完全不可能进行延迟加载)。在代理受到干扰的特定情况下(例如序列化)禁用代理创建(也禁用延迟加载)就足够了:

ppEFContext.Configuration.ProxyCreationEnabled = false;

这只会为特定的上下文实例ppEFContext禁用代理创建。

(我刚刚看到,@WillC 已经在这里提到过。请为他的回答点赞。)

关于c# - EF 4.1 - 代码优先 - JSON 循环引用序列化错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5588143/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com