自引用循环使用Angular 2检测Asp.net MVC中的属性

时间:2021-09-10 20:38:16
  1. I Have Create a DB in that I am Having Multiple tables having Relationship between them.
  2. 我创建了一个数据库,因为我有多个表,它们之间有关系。
  3. 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"

    “'使用类型'System.Data.Entity.DynamicProxies.PrescriptionMaster_2C4C63F6E22DFF8E29DCAC8D06EBAE038831B58747056064834E80E41B5C4E4A'检测到自引用循环。路径'[0] .Patient.PrescriptionMasters”

  4. I coudn't get why i am getting this error, and when i remove the relationships between tables i get proper data From it.
  5. 我不明白为什么我得到这个错误,当我删除表之间的关系时,我从中得到适当的数据。
  6. 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;”

    in Webconfig.cs but nothing has worked for me.

    在Webconfig.cs但没有任何东西对我有用。

Please help me, what should I do ?

请帮帮我,我该怎么办?

2 个解决方案

#1


3  

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

防止这种情况发生的唯一正确方法是不将实体框架对象(可能包含这样的循环)发送到JSON Serializer(它不太知道何时停止序列化)。

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.

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

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.

一个好的经验法则是仅将EF对象中的简单属性(数字,布尔值,字符串,日期时间)分配给ViewModel项。一旦遇到另一个EF对象(或EF对象集合)的EF对象属性,您需要将这些属性转换为未链接到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.

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

#2


0  

Just add this to the Application_Start in Global.asax:

只需将其添加到Global.asax中的Application_Start:

HttpConfiguration config = GlobalConfiguration.Configuration;

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

It will ignore the reference pointing back to the object.

它将忽略指向对象的引用。

#1


3  

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

防止这种情况发生的唯一正确方法是不将实体框架对象(可能包含这样的循环)发送到JSON Serializer(它不太知道何时停止序列化)。

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.

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

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.

一个好的经验法则是仅将EF对象中的简单属性(数字,布尔值,字符串,日期时间)分配给ViewModel项。一旦遇到另一个EF对象(或EF对象集合)的EF对象属性,您需要将这些属性转换为未链接到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.

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

#2


0  

Just add this to the Application_Start in Global.asax:

只需将其添加到Global.asax中的Application_Start:

HttpConfiguration config = GlobalConfiguration.Configuration;

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

It will ignore the reference pointing back to the object.

它将忽略指向对象的引用。