在带有Angular 2的Asp.net MVC中检测到自引用循环的属性 [英] Self referencing loop Detected for property in Asp.net MVC with Angular 2

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

问题描述

  1. 我已经创建了一个数据库,因为我有多个表,它们之间具有关系.
  2. 当尝试从我的WEb应用程序获取数据时,出现此错误
  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

"'检测到类型为'System.Data.Entity.DynamicProxies.PrescriptionMaster_2C4C63F6E22DFF8E29DCAC8D06EBAE038831B587470560648648E34E80E41B5C4E4A的自引用循环.路径'[0] .Patient.PrescriptionMasters"

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

  • 我不知道为什么会收到此错误,并且当我删除表之间的关系时,也会从中获取适当的数据.
  • 我尝试了其他解决方案,例如添加

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

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

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

    在Webconfig.cs中,但对我没有任何帮助.

    in Webconfig.cs but nothing has worked for me.

    请帮助我,我该怎么办?

    Please help me, what should I do ?

    推荐答案

    防止这种情况发生的唯一正确方法是不发送Entity Framework对象(可能包含此类循环)进入JSON序列化程序(不太了解何时停止序列化).

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

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

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

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

    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.

    在频谱的另一端,有一些库,例如AutoMapper.如果您决定需要实际的ViewModel类,则AutoMapper将以非常结构化的方式帮助将EF对象映射到那些ViewModel.

    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中检测到自引用循环的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆