使用 Angular 2 在 Asp.net MVC 中检测到自引用循环

Self referencing loop Detected for property in Asp.net MVC with Angular 2(使用 Angular 2 在 Asp.net MVC 中检测到自引用循环)

本文介绍了使用 Angular 2 在 Asp.net MVC 中检测到自引用循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  1. 我创建了一个数据库,因为我有多个表之间存在关系.
  2. 尝试从我的 WEB 应用程序获取数据时出现此错误<块引用>

    "'检测到类型为 'System.Data.Entity.DynamicProxies.PrescriptionMaster_2C4C63F6E22DFF8E29DCAC8D06EBAE038831B58747056064834E80E41B5C4E4A'的自引用循环.PathMaster'[pPrescription]

  3. 我不明白为什么我会收到这个错误,当我删除表之间的关系时,我从中获得了正确的数据.
  4. 我尝试了其他解决方案,例如添加

    "config.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling= Newtonsoft.Json.ReferenceLoopHandling.Ignore;"

    在 Webconfig.cs 中,但对我没有任何作用.

请帮帮我,我该怎么办?

解决方案

防止这种情况发生的唯一正确方法是不发送实体框架对象(可能包含此类循环)进入 JSON 序列化器(不太擅长知道何时停止序列化).

相反,创建模拟前端实际需要的 EF 对象部分的 ViewModel,然后使用 EF 对象填充这些 ViewModel.

一种快速而肮脏的方法是只使用匿名对象,例如:

返回新的{产品 = 新{Id = EF_Product.Id,名称 = EF_Product.Name}};

一个好的经验法则是仅将 EF 对象中的简单属性(数字、布尔值、字符串、日期时间)分配给 ViewModel 项.一旦遇到作为另一个 EF 对象(或 EF 对象的集合)的 EF 对象属性,那么您也需要将它们转换为未链接到 EF 的简单"对象.

另一方面,还有诸如 AutoMapper 之类的库.如果您决定需要实际的 ViewModel 类,那么 AutoMapper 将帮助以非常结构化的方式将 EF 对象映射到这些 ViewModel.

  1. I Have Create a DB in that I am Having Multiple tables having Relationship between them.
  2. When a try to get data from my WEb app i get this error

    "'Self referencing loop detected with type 'System.Data.Entity.DynamicProxies.PrescriptionMaster_2C4C63F6E22DFF8E29DCAC8D06EBAE038831B58747056064834E80E41B5C4E4A'. Path '[0].Patient.PrescriptionMasters"

  3. I coudn't get why i am getting this error, and when i remove the relationships between tables i get proper data From it.
  4. I have Tried other solutions like adding

    "config.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore; "

    in Webconfig.cs but nothing has worked for me.

Please help me, what should I do ?

解决方案

The only proper way to prevent this from happening is by not sending Entity Framework objects (which may contain such loops) into the JSON Serializer (which is not too good at knowing when to stop serializing).

Instead, create ViewModels that mimic the parts of the EF Objects that your Front End actually needs, then fill those ViewModels using the EF Objects.

A quick-and-dirty way is to just use anonymous objects, for example:

return new
{
    Product = new
    {
        Id = EF_Product.Id,
        Name = EF_Product.Name
    }
};

A good rule-of-thumb is to only assign simple properties (number, bool, string, datetime) from the EF Objects to the ViewModel items. As soon as you encounter an EF Object property that is yet another EF Object (or a collection of EF Objects), then you need to translate those as well to 'simple' objects that are not linked to EF.

On the other end of the spectrum there are libraries such as AutoMapper. If you decide that you need actual ViewModel classes, then AutoMapper will help mapping the EF Objects to those ViewModels in a very structured way.

这篇关于使用 Angular 2 在 Asp.net MVC 中检测到自引用循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:使用 Angular 2 在 Asp.net MVC 中检测到自引用循环

基础教程推荐