首页 > 其他分享 >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

时间:2022-11-28 13:13:34浏览次数:71  
标签:body Core duplicates graph EF JSON POST example

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/… – joakimriedel 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:

 

 

标签:body,Core,duplicates,graph,EF,JSON,POST,example
From: https://www.cnblogs.com/chucklu/p/16931915.html

相关文章

  • 拓端tecdat|R语言中的多类别问题的绩效衡量:F1-score 和广义AUC
    R语言中的多类别问题的绩效衡量:F1-score和广义AUC 对于分类问题,通常根据与分类器关联的混淆矩阵来定义分类器性能。根据混淆矩阵,可以计算灵敏度......
  • postman的使用
    一、使用介绍1、创建集合  集合可以理解成请求的总和或合集,在Postman中,集合表示将请求进行分组、分模块管理;对含义相近、对功能相近的请求保存到一个集合中,方便后期......
  • 《ASP.NET Core技术内幕与项目实战》精简集-DDD准备5.5:集成事件RabbitMQ
     本节内容,部分为补充内容,部分涉及到9.3.10-9.3.12(P335-342)。主要NuGet包:RabbitMQ.Client 微服务间,跨进程的事件发布和订阅,需要借助第三方服务器作为事件总线,目前常......
  • uniapp表单提交 新增POST
    用button的type=submit和form<template><view><viewclass="info"><form@submit='formSubmit'><viewclass="list"><viewclass="item">......
  • 基于.NetCore开发博客项目 StarBlog - (20) 图片显示优化
    前言我的服务器带宽比较高,博客部署在上面访问的时候几乎没感觉有加载延迟,就没做图片这块的优化,不过最近有小伙伴说博客的图片加载比较慢,那就来把图片优化完善一下吧~目前......
  • .NET Core中的AOP
    1.AOP的应用场景AOP全称AspectOrientedProgarmming(面向切面编程),其实AOP对ASP.NET程序员来说一点都不神秘,你也许早就通过Filter来完成一些通用的功能,例如你使用Authori......
  • lightdb/postgresql中的事务回卷原理解析及避免
    在pg中,由于事务id采用32位实现,所以是采用循环复用的,如下:  虽然最大支持4billion个事务(32位无符号数,xid最大可以到40亿),但是新老事务相差2billion是上限,当达......
  • ASP.NET Core教程-Model Binding(模型绑定)
    更新记录转载请注明出处:2022年11月27日发布。2022年11月25日从笔记迁移到博客。模型绑定是什么模型绑定是指:使用来自HTTP请求的值来创建.NET对象的过程。模型绑......
  • 会话丢失-NGINX配置之underscores_in_headers
    1.描述问题NGINX代理某个web服务时,单机情况下也出现不停的要求认证的情况初步分析去掉NGINX代理,直接访问服务,未出现上述情况;进一步分析:查看经过NGINX的请求和直接访问服......
  • 1527_AURIX_TriCore内核架构开篇与架构概述
    全部学习汇总:​​GreyZhang/g_tricore_architecture:somelearningnoteabouttricorearchitecture.(github.com)​​看文档的时候,引用了内核架构的内容。这方面我没有......