EF 4.1 - 代码优先 - JSON 循环引用序列化错误
我收到循环引用序列化错误,但据我所知,我没有任何循环引用。我正在从数据库中检索一组订单并将它们作为 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);
}
谢谢
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
您可以尝试从所有导航属性中删除
virtual
关键字以禁用延迟加载和代理创建,然后使用预先加载来显式加载所需的对象图:参考您的 上一篇文章 看起来您的应用程序不依赖无论如何,在延迟加载上,因为您引入了虚拟属性来延迟加载对象图,现在可能会导致序列化问题。
编辑
没有必要从导航属性中删除
virtual
关键字(这会使模型完全不可能进行延迟加载)。对于代理令人不安的特定情况(例如序列化)禁用代理创建(也禁用延迟加载)就足够了:这仅针对特定上下文实例
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: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: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.)
当您知道需要从特定上下文进行序列化时,您可以为该特定查询禁用代理创建,如下所示。这对我很有用,比修改我的模型类更好。
这种方法消除了上下文的这个特定实例的代理对象类型,因此返回的对象是实际的类,因此序列化不是问题。
即:
代替
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.
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:
instead of
问题是您实际上正在序列化实体框架生成的代理对象。不幸的是,与 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.
有一个属性可以添加到实体框架对象中,
这使得代码不执行循环引用。
There is an attribute to add to Entity Framework objects
This makes the code not perform Circular references.
我认为他们已经在最新版本中修复了这个问题。
查看“序列化和反序列化 JSON ->”部分下的帮助文档 ; 序列化和保留对象引用”。
在初始化 JSON.Net 序列化器时设置此设置:
所以一个例子是这样的:
我验证了这适用于我的代码优先解决方案,以及导航属性中的循环引用。如果您查看生成的 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:
So an example would be this:
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.
另一种解决方案是使用匿名类型作为 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.
如果只需要对象中的某些值,另一种解决方案是构建一个匿名类并返回它,如下例所示:
An alternative solution, if only some values from objects are necessary, is build an anonymous class and return it, like the example below:
发生循环引用是因为您对对象使用了预加载。
您有几种方法:
DbContext.Configuration.ProxyCreationEnabled = false;
PS。代理是从实体框架加载 EF 时创建的对象。简而言之:这意味着它保存原始值和更新后的值,以便以后可以更新它们。它处理其他事情;-)
The circular reference happens because you use eager loading on the object.
You have a couple of methods:
DbContext.Configuration.ProxyCreationEnabled = false;
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 ;-)
对于那些使用代理 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.
您可以删除
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.
我能够使用此处描述的方法解决此问题:
http://mytechworld.officeacuity.com/index.php/2010/02/serializing-entity-framework-objects-into-json-using-asp-net-mvc/
I was able to solve this problem by using the method described here:
http://mytechworld.officeacuity.com/index.php/2010/02/serializing-entity-framework-objects-into-json-using-asp-net-mvc/