EF Core | Passing navigation properties in JSON body to API controller as POST request

EF Core | Passing navigation properties in JSON body to API controller as POST request

Here's the official docs on avoiding graph cycles in JSON: learn.microsoft.com/en-us/ef/core/change-tracking/… May 23 at 8:28
 
 

Attaching a serialized graph

EF Core works with graphs of entities connected via foreign keys and navigation properties, as described in Changing Foreign Keys and Navigations. If these graphs are created outside of EF Core using, for example, from a JSON file, then they can have multiple instances of the same entity. These duplicates need to be resolved into single instances before the graph can be tracked.

Graphs with no duplicates

Before going any further it is important to recognize that:

  • Serializers often have options for handling loops and duplicate instances in the graph.
  • The choice of object used as the graph root can often help reduce or remove duplicates.

If possible, use serialization options and choose roots that do not result in duplicates. For example, the following code uses Json.NET to serialize a list of blogs each with its associated posts:

 

 

Handling duplicates

The code in the previous example serialized each blog with its associated posts. If this is changed to serialize each post with its associated blog, then duplicates are introduced into the serialized JSON. For example:

Preserve references

Json.NET provides the PreserveReferencesHandling option to handle this. For example:

C#
var serialized = JsonConvert.SerializeObject(
    posts,
    new JsonSerializerSettings
    {
        PreserveReferencesHandling = PreserveReferencesHandling.All, Formatting = Formatting.Indented
    });

The resulting JSON now looks like this:

 

 

Notice that this JSON has replaced duplicates with references like "$ref": "5" that refer to the already existing instance in the graph. This graph can again be tracked using the simple calls to Update, as shown above.

The System.Text.Json support in the .NET base class libraries (BCL) has a similar option which produces the same result. For example:

var serialized = JsonSerializer.Serialize(
    posts, new JsonSerializerOptions { ReferenceHandler = ReferenceHandler.Preserve, WriteIndented = true });

 

 

Resolve duplicates

If it is not possible to eliminate duplicates in the serialization process, then ChangeTracker.TrackGraph provides a way to handle this. TrackGraph works like Add, Attach and Update except that it generates a callback for every entity instance before tracking it. This callback can be used to either track the entity or ignore it. For example:

 

 

作者:Chuck Lu    GitHub    
posted @   ChuckLu  阅读(29)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2018-11-28 单元测试的基本概念
2018-11-28 File.Copy的时候Could not find a part of the path
2018-11-28 xunit inlinedata classdata memberdata
2017-11-28 Adding Kentico controls to the Visual Studio toolbox
2015-11-28 git lfs
2015-11-28 使用git对unity3d项目进行版本控制
2015-11-28 从unity3d官网下载教程
点击右上角即可分享
微信分享提示