JSON.NET Error Self referencing loop detected for type

后端 未结 25 2804
我在风中等你
我在风中等你 2020-11-22 02:16

I tried to serialize POCO class that was automatically generated from Entity Data Model .edmx and when I used

JsonConvert.SerializeObject 

相关标签:
25条回答
  • 2020-11-22 02:28

    People have already talked about [JsonIgnore] being added to the virtual property in the class, for example:

    [JsonIgnore]
    public virtual Project Project { get; set; }
    

    I will also share another option, [JsonProperty(NullValueHandling = NullValueHandling.Ignore)] which omits the property from serialization only if it is null:

    [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
    public virtual Project Project { get; set; }
    
    0 讨论(0)
  • 2020-11-22 02:30

    If you're using .NET Core 2.x, update your ConfigureServices section in Startup.cs

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

        public void ConfigureServices(IServiceCollection services)
        {
        ...
    
        services.AddMvc()
            .AddJsonOptions(
                options => options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
            );
    
        ...
        }
    

    If you're using .NET Core 3.x without MVC, it would be:

    services.AddControllers()
      .AddNewtonsoftJson(options =>
          options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
       );
    

    This reference loop handling is almost mandatory if you're using Entity Framework and database-first design pattern.

    0 讨论(0)
  • 2020-11-22 02:30

    To ignore loop references and not to serialize them globally in MVC 6 use the following in startup.cs:

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc().Configure<MvcOptions>(options =>
            {
                options.OutputFormatters.RemoveTypesOf<JsonOutputFormatter>();
                var jsonOutputFormatter = new JsonOutputFormatter();
                jsonOutputFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
                options.OutputFormatters.Insert(0, jsonOutputFormatter);
            });
        }
    
    0 讨论(0)
  • 2020-11-22 02:30

    For not looping this worked for me-
    ReferenceLoopHandling = ReferenceLoopHandling.Ignore,

    I've solved it all here - Entity Framework children serialization with .Net Core 2 WebAPI https://gist.github.com/Kaidanov/f9ad0d79238494432f32b8407942c606

    Will appreciate any remarks. maybe someone can use it sometime.

    0 讨论(0)
  • 2020-11-22 02:31

    The simplest way to do this is to install Json.NET from nuget and add the [JsonIgnore] attribute to the virtual property in the class, for example:

        public string Name { get; set; }
        public string Description { get; set; }
        public Nullable<int> Project_ID { get; set; }
    
        [JsonIgnore]
        public virtual Project Project { get; set; }
    

    Although these days, I create a model with only the properties I want passed through so it's lighter, doesn't include unwanted collections, and I don't lose my changes when I rebuild the generated files...

    0 讨论(0)
  • 2020-11-22 02:31

    For .NET Core 3.0, update the Startup.cs class as shown below.

    public void ConfigureServices(IServiceCollection services)
    {
    ...
    
    services.AddControllers()
        .AddNewtonsoftJson(
            options => options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
        );
    
    ...
    }
    

    See: https://devblogs.microsoft.com/aspnet/asp-net-core-updates-in-net-core-3-0-preview-5/

    0 讨论(0)
提交回复
热议问题