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

发布于 2024-10-31 05:51:42 字数 4205 浏览 1 评论 0 原文

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

这是错误:

错误

检测到循环引用 序列化类型的对象时 'System.Data.Entity.DynamicProxies.Order_83CECF2AA4DE38232F9077D4B26941AB96BC61230419EA8AC42C9100E6072812'。 描述:未处理的异常 执行期间发生的 当前的网络请求。请查看 堆栈跟踪以获取有关的更多信息 错误及其起源 代码。

异常详细信息: System.InvalidOperationException:A 检测到循环引用 序列化类型的对象 'System.Data.Entity.DynamicProxies.Order_83CECF2AA4DE38232F9077D4B26941AB96BC61230419EA8AC42C9100E6072812'。

来源错误:

生成了未处理的异常 当前执行期间 网络请求。有关信息 异常的起源和地点 可以使用异常来识别 下面的堆栈跟踪。

我的课程如下:

订单

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

}

注意:地址类别是编辑期间新增的

地址

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

执行序列化的代码位于此处:

摘自 OrderController

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

谢谢

I am getting an a Circular Reference Serialization Error although, to my knowledge I do not have any circular references. I am retrieving a set of Orders from the database and sending them to the client as JSON. All the code is shown below.

This is the error:

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.

My classes are as follows:

Order

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

Patient

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

Certification Period

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

Agency

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

Diagnosis

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

User

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

Address

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

The code that executes the serialization is here:

Excerpt from OrderController

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

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

南汐寒笙箫 2024-11-07 05:51:42

您可以尝试从所有导航属性中删除 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);
}

参考您的 上一篇文章 看起来您的应用程序不依赖无论如何,在延迟加载上,因为您引入了虚拟属性来延迟加载对象图,现在可能会导致序列化问题。

编辑

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

ppEFContext.Configuration.ProxyCreationEnabled = false;

这仅针对特定上下文实例 ppEFContext 禁用代理创建。

(我刚刚看到,@WillC 已经在这里提到过。请为他的回答投票支持此编辑。)

You could try to remove the virtual keyword from all navigation properties to disable lazy loading and proxy creation and then use eager loading instead to load the required object graph explicitely:

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

Referring to your previous post it looks like your application isn't relying on lazy loading anyway because you introduced there the virtual properties to load the object graph lazily, possibly causing now the serialization trouble.

Edit

It's not necessary to remove the virtual keyword from the navigation properties (which would make lazy loading completely impossible for the model). It's enough to disable proxy creation (which disables lazy loading as well) for the specific circumstances where proxies are disturbing, like serialization:

ppEFContext.Configuration.ProxyCreationEnabled = false;

This disables proxy creation only for the specific context instance ppEFContext.

(I've just seen, @WillC already mentioned it here. Upvote for this edit please to his answer.)

记忆消瘦 2024-11-07 05:51:42

当您知道需要从特定上下文进行序列化时,您可以为该特定查询禁用代理创建,如下所示。这对我很有用,比修改我的模型类更好。

using (var context = new MeContext())
{
    context.Configuration.ProxyCreationEnabled = false;
    return context.cars.Where(w => w.Brand == "Ferrari")
}

这种方法消除了上下文的这个特定实例的代理对象类型,因此返回的对象是实际的类,因此序列化不是问题。

即:

{Models.car} 

代替

{System.Data.Entity.DynamicProxies.car_231710A36F27E54BC6CE99BB50E0FE3B6BD4462EC‌​A19695CD1BABB79605296EB} 

When you know that you need to serialize from a particular context, you can disable proxy creation for that particular query like below. This has worked for me and is better than revising my model classes.

using (var context = new MeContext())
{
    context.Configuration.ProxyCreationEnabled = false;
    return context.cars.Where(w => w.Brand == "Ferrari")
}

This approach takes away the proxy object type for this particular instance of the context so the returned objects are the actual class and therefore serialization is not a problem.

ie:

{Models.car} 

instead of

{System.Data.Entity.DynamicProxies.car_231710A36F27E54BC6CE99BB50E0FE3B6BD4462EC‌​A19695CD1BABB79605296EB} 
水晶透心 2024-11-07 05:51:42

问题是您实际上正在序列化实体框架生成的代理对象。不幸的是,与 JSON 序列化器一起使用时会出现一些问题。为了 JSON 兼容性,您可能会考虑将实体映射到特殊的简单 POCO 类。

The problem is that your are actually serializing an entity framework generated proxy object. Unfortunatly this has some issues when used with the JSON serializer. You might consider to map your entities to special simple POCO classes for the sake of JSON compatibility.

辞慾 2024-11-07 05:51:42

有一个属性可以添加到实体框架对象中,

[ScriptIgnore]

这使得代码不执行循环引用。

There is an attribute to add to Entity Framework objects

[ScriptIgnore]

This makes the code not perform Circular references.

澉约 2024-11-07 05:51:42

我认为他们已经在最新版本中修复了这个问题。

查看“序列化和反序列化 JSON ->”部分下的帮助文档 ; 序列化和保留对象引用”。

在初始化 JSON.Net 序列化器时设置此设置:

PreserveReferencesHandling = PreserveReferencesHandling.Objects;

所以一个例子是这样的:

var serializerSettings = new JsonSerializerSettings { PreserveReferencesHandling = PreserveReferencesHandling.Objects };

string json = JsonConvert.SerializeObject(people, Formatting.Indented, serializerSettings);

我验证了这适用于我的代码优先解决方案,以及导航属性中的循环引用。如果您查看生成的 JSON,它应该到处都有“$id”和“$ref”属性。

I think they have fixed this in the latest version.

Check out the help docs under the section "Serializing and Deserializing JSON -> Serialization and Preserving Object References".

Set this setting when initializing the JSON.Net Serializer:

PreserveReferencesHandling = PreserveReferencesHandling.Objects;

So an example would be this:

var serializerSettings = new JsonSerializerSettings { PreserveReferencesHandling = PreserveReferencesHandling.Objects };

string json = JsonConvert.SerializeObject(people, Formatting.Indented, serializerSettings);

I verified that this works with my code first solution, and a circular reference in the navigation properties. If you look at the resulting JSON it should have "$id" and "$ref" properties everywhere.

情绪操控生活 2024-11-07 05:51:42

另一种解决方案是使用匿名类型作为 LINQ 查询的结果。

在我的项目中,我广泛使用延迟加载,禁用它不是正确的做法。

An alternative solution would be to use anonymous types as the result of a LINQ query.

In my project, I am using lazy loading extensively, and disabling it was not the right thing to do.

许你一世情深 2024-11-07 05:51:42

如果只需要对象中的某些值,另一种解决方案是构建一个匿名类并返回它,如下例所示:

public JsonResult AjaxFindByName(string term)
{
    var customers = context.Customers
        .Where(c => c.Name.ToUpper().Contains(term.ToUpper())).Take(10)
        .AsEnumerable()
        .Select(c => new { 
            value = c.Name, 
            SSN = String.Format(@"{0:000\-00\-0000}", c.SSN),
            CustomerID = c.CustomerID });

    return Json(customers, JsonRequestBehavior.AllowGet);
}

An alternative solution, if only some values from objects are necessary, is build an anonymous class and return it, like the example below:

public JsonResult AjaxFindByName(string term)
{
    var customers = context.Customers
        .Where(c => c.Name.ToUpper().Contains(term.ToUpper())).Take(10)
        .AsEnumerable()
        .Select(c => new { 
            value = c.Name, 
            SSN = String.Format(@"{0:000\-00\-0000}", c.SSN),
            CustomerID = c.CustomerID });

    return Json(customers, JsonRequestBehavior.AllowGet);
}
旧时模样 2024-11-07 05:51:42

发生循环引用是因为您对对象使用了预加载。

您有几种方法:

  • 加载查询时关闭急切加载(linq 或 lambda)
    DbContext.Configuration.ProxyCreationEnabled = false;
  • 从域模型中删除 virtual 关键字
  • 分离对象(= 无急切加载功能且无代理)
    • Repository.Detach(entityObject)
    • DbContext.Entry(entityObject).EntityState = EntityState.Detached
  • 克隆属性
    • 您可以使用 AutoMapper 之类的工具来克隆对象,不要使用 ICloneable 接口,因为它还会克隆对象中的 ProxyProperties,因此这是行不通的。
  • 如果您正在构建 API,请尝试使用具有不同配置(不返回代理)的单独项目

PS。代理是从实体框架加载 EF 时创建的对象。简而言之:这意味着它保存原始值和更新后的值,以便以后可以更新它们。它处理其他事情;-)

The circular reference happens because you use eager loading on the object.

You have a couple of methods:

  • Turn off eager loading when your loading your Query (linq or lambda)
    DbContext.Configuration.ProxyCreationEnabled = false;
  • Remove the virtual keyword from the Domainmodel
  • Detach the objects (= no eager loading functionality & no proxy)
    • Repository.Detach(entityObject)
    • DbContext.Entry(entityObject).EntityState = EntityState.Detached
  • Clone the properties
    • You could use something like AutoMapper to clone the object, don't use the ICloneable interface, because it also clones the ProxyProperties in the object, so that won't work.
  • In case you are building an API, try using a separte project with a different configuration (that doesn't return proxies)

PS. Proxies is the object that's created by EF when you load it from the Entity Framework. In short: It means that it holds the original values and updated values so they can be updated later. It handles other things to ;-)

绝影如岚 2024-11-07 05:51:42

对于那些使用代理 EF/Linq2SQL 类的人,我的解决方案是简单地删除子实体上的父引用。

因此,在我的模型中,我选择了关系并将父引用更改为内部引用而不是公共引用。

可能不是所有人的理想解决方案,但对我有用。

For those using the proxy EF/Linq2SQL classes my solution was to simply remove the parent reference on my child entities.

So in my model, I selected the relationship and changed the Parent reference to be Internal rather than Public.

May not be an ideal solution for all, but worked for me.

﹏雨一样淡蓝的深情 2024-11-07 05:51:42

您可以删除virtual关键字:

public virtual Patient Patient { get;放; } -> 公共病人病人{ get;放; }

请记住,当您删除 virtual 关键字时,延迟加载将被关闭。

You can remove the virtual keyword:

public virtual Patient Patient { get; set; } -> public Patient Patient { get; set; }

Keep in mind that when you remove the virtual keyword, lazy loading will be turned off.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文