简体   繁体   English

使用InverseProperty时,Entity Framework Core模型中的自引用循环

[英]Self referencing loop in Entity Framework Core models when using InverseProperty

I have the following models: 我有以下型号:

  public class MasterData
  {
    [Key]
    public Guid ID { get; set; }

    [Required]
    public string Name { get; set; } 

    [InverseProperty(nameof(DetailData.MasterData))]
    public List<Detaildata> Details { get; set; }
  }

  public class DetailData
  {
    [Key]
    public Guid ID { get; set; }

    [Required]
    public string Name { get; set; }

    [InverseProperty(nameof(ChildData.DetailData))]
    public List<ChildData> Children { get; set; }

    public Guid MasterDataID { get; set; }

    [ForeignKey(nameof(MasterDataID))]
    public MasterData MasterData { get; set; }
  }


  public class ChildData
  {
    [Key]
    public Guid ID { get; set; }

    [Required]
    public string Name { get; set; }

    public Guid SectionID { get; set; }

    [ForeignKey(nameof(SectionID))]
    public Section ChildSection { get; set; }

    public Guid DetailDataID { get; set; }

    [ForeignKey(nameof(DetailDataID))]
    public DetailData DetailData { get; set; }
  }

To make sure that the migration is well generated and to avoid shadow properties , I add the InverseProperty attribute to define both ends of the relationship. 为了确保正确生成迁移并避免阴影属性 ,我添加了InverseProperty属性来定义关系的两端。

However, when I try to serialize the instances, I get the following exception: 但是,当我尝试序列化实例时,出现以下异常:

Self referencing loop detected for property 'DetailData' with type 'Models.DetailData'. 为类型为“ Models.DetailData”的属性“ DetailData”检测到自引用循环。 Path 'Value.Item1.Details[0].Children[0]'. 路径'Value.Item1.Details [0] .Children [0]'。

Well, obviously it is a self referencing loop, as MasterData has a DetailData that has a MasterData navigation object back - as it should be in a navigational relationship. 好吧,显然这是一个自引用循环,因为MasterData具有一个DetailData ,后者具有一个MasterData导航对象,因为它应该处于导航关系中。

Unfortunately, I need to serialize these objects. 不幸的是,我需要序列化这些对象。

A workaround would be to mark the InverseProperty properties with the JsonIgnore attribute, but I'm not sure what it would cause in the deserializing side of the project. 一种解决方法是使用InverseProperty属性标记JsonIgnore属性,但是我不确定这会在项目的反序列化方面导致什么。

What should I do to avoid the self referencing loop without ignoring the navigation properties? 我该怎么做才能避免在不忽略导航属性的情况下发生自引用循环?

You could ignore circular reference globally by editing your WebApiConfig.cs and putting the following code: 您可以通过编辑WebApiConfig.cs并添加以下代码来全局忽略循环引用:

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

If you're using .NET Core edit the Startup.cs to add 如果您使用的是.NET Core,请编辑Startup.cs以添加

var mvc = services.AddMvc(options =>
        {
           ...
        })
        .AddJsonOptions(x => x.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore);

It is possible yet to use annotations to solve this by putting [JsonIgnore] on the Details property. 通过将[JsonIgnore]放在Details属性上,可能还可以使用注释来解决此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM