添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

I received this message but I don't know really what does it mean!

Newtonsoft.Json.JsonSerializationException: Self referencing loop detected for property 'suiviBE' with type 'FollowUpDash.Shared.Models.SuiviBE'. Path '[0].actionItems[0]'.

To fix it I add in program.cs

builder.Services.AddControllers().AddNewtonsoftJson(options =>
    options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
    options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;

But I don't know what' happen in my code. Have you an idea why I received this message?

Thanks in advance!

public int Id { get; set; } public string Title { get; set; } public string Content { get; set; } public virtual Blog Blog { get; set; }

Because EF Core automatically does fix-up of navigation properties, you can end up with cycles in your object graph. For example, loading a blog and its related posts will result in a blog object that references a collection of posts. Each of those posts will have a reference back to the blog.

Some serialization frameworks don't allow such cycles. For example, Json.NET will throw the following exception if a cycle is found.

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

More detail information, see Related data and serialization.

If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

Best regards,

Dillion

Thanks to your explanation.

So as you can see in first post I was able to fix the problem by adding in program.cs below

builder.Services.AddControllers().AddNewtonsoftJson(options =>
    options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
    options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;

What do you think about that?

Can I stay with that or do you have change the code?

What do you think about that? Can I stay with that or do you have change the code?

There are issues with using Entities as a Web API interface.

The design opens up Web API for over posting. Entities model the table schema. It is very common to have an update scenario where the user updates only a few of the column not all columns in a table. This can cause column values to get overwritten by default values. It also allows malicious actors to add values to the post that you did not expect.

https://learn.microsoft.com/en-us/aspnet/core/mvc/models/model-binding?view=aspnetcore-7.0

Another issue is unexpected validation errors. This happens when Entities contain navigation properties. Let's assume the intent is to add or update an Entity that has a child relationship. The add or update can cause a validation error in the child record. For example, if there a required field or a non-null property. Now you're faced with balancing model validation with table schema design.

The solution is to use View Models and DTOs. Please see your prior threads with these issues and the community's recommendations.