在 ASP.Net 核心 1.1 解决方案中检测到自引用循环

Self referencing loop detected in ASP.Net Core 1.1 Solution

尽管我已经按照文章进行操作,但我仍然收到错误

self referencing loop detected for property '...' with type '...'. Path '[4]....[0]'.

我已将此添加到我的 Startup.cs:

services.AddMvc()
    .AddJsonOptions(opt => 
        opt.SerializerSettings.ReferenceLoopHandling = 
            ReferenceLoopHandling.Ignore
    );

还有什么可能导致参考回路错误?

编辑: 回答评论中的问题... 受影响的 类 是:

public partial class GuidingSymptom
    {
        public GuidingSymptom()
        {
            VideosGuidingSymptoms = new HashSet<VideosGuidingSymptoms>();
        }
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }
        [MaxLength(70)]
        [Required]
        public string Name { get; set; }
        public string Description { get; set; }

        public int SeverityId { get; set; }
        public int? DiagnoseId { get; set; }

        [InverseProperty("GuidingSymptom")]
        public virtual ICollection<VideosGuidingSymptoms> VideosGuidingSymptoms { get; set; }
        [ForeignKey("DiagnoseId")]
        [InverseProperty("GuidingSymptom")]
        public virtual Diagnose Diagnose { get; set; }
        [ForeignKey("SeverityId")]
        [InverseProperty("GuidingSymptom")]
        public virtual GuidingSymptomSeverity Severity { get; set; }
    }

public partial class VideosGuidingSymptoms
{
    public int VideoId { get; set; }
    public int GuidingSymptomId { get; set; }

    [ForeignKey("GuidingSymptomId")]
    [InverseProperty("VideosGuidingSymptoms")]
    public virtual GuidingSymptom GuidingSymptom { get; set; }
    [ForeignKey("VideoId")]
    [InverseProperty("VideosGuidingSymptoms")]
    public virtual Video Video { get; set; }
}

我找到了添加

的解决方案
[JsonIgnore]

受影响的注释 属性。但是,我预计在使用 ReferenceLoopHandling.Ignore

时不需要这样做

一些序列化框架不允许这样的循环。比如Json.NET遇到循环就会抛出如下异常

Newtonsoft.Json.JsonSerializationException: Self referencing loop detected for property 'Blog' with type 'MyApplication.Models.Blog'.

如果您正在使用 ASP.NET 核心,您可以配置 Json.NET 以忽略它在对象图中找到的循环。这是在 Startup.cs.

中的 ConfigureServices(...) 方法中完成的
public void ConfigureServices(IServiceCollection services)
{
    ...

    services.AddMvc()
        .AddJsonOptions(
            options => options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
        );

    ...
}

https://docs.microsoft.com/en-us/ef/core/querying/related-data