ef core 自引用类 报错System.Text.Json.JsonException: A possible object cycle was detected which is not supported....

ef core 创建自引用灰了了

public class Menu:IEntity
{
    public int Id { get; set; }
    public string text { get; set; }
    public bool group { get; set; }
    public bool shortout_root { get; set; }
    public string link { get; set; }
    public string icon { get; set; }
    public bool linkExact { get; set; }
    public virtual List<Menu> children { get; set; }
     public virtual  int? parentId { get; set; }
     public virtual  Menu? parent { get; set; }
    public List<UserMenu> UserMenu { get; set; }
}

报错:System.Text.Json.JsonException: A possible object cycle was detected which is not supported. This can either be due to a cycle or if the object depth is larger than the maximum allowed depth of 32

检测到不支持的可能对象循环。这可能是由于周期或物体深度大于最大允许深度32。

解决:添加 Microsoft.AspNetCore.Mvc.NewtonsoftJson 包后正常,但引用的深度就比较深了

id: 8
text: "Test"
group: false
shortout_root: false
link: "Test"
icon: null
linkExact: false
children: null
parentId: 5
parent: {id: 5, text: "CMS", group: false, shortout_root: false, link: null, icon: "anticon anticon-skin",…}
id: 5
text: "CMS"
group: false
shortout_root: false
link: null
icon: "anticon anticon-skin"
linkExact: false
children: [{id: 6, text: "CMS列表", group: false, shortout_root: false, link: "/cms/cmslist", icon: null,…},…]
0: {id: 6, text: "CMS列表", group: false, shortout_root: false, link: "/cms/cmslist", icon: null,…}
id: 6
text: "CMS列表"
group: false
shortout_root: false
link: "/cms/cmslist"
icon: null
linkExact: false
children: null
parentId: 5
userMenu: null
1: {id: 7, text: "模块列表", group: false, shortout_root: false, link: "cms/modulelist", icon: null,…}
id: 7
text: "模块列表"
group: false
shortout_root: false
link: "cms/modulelist"
icon: null
linkExact: false
children: null
parentId: 5
userMenu: null
parentId: 2
parent: {id: 2, text: "业务", group: true, shortout_root: false, link: null, icon: null, linkExact: false,…}
id: 2
text: "业务"
group: true
shortout_root: false
link: null
icon: null
linkExact: false
children: []
parentId: null
parent: null
userMenu: null
userMenu: null
userMenu: null

原文地址:https://www.cnblogs.com/tiandidao/p/12153704.html

时间: 2024-08-29 16:21:48

ef core 自引用类 报错System.Text.Json.JsonException: A possible object cycle was detected which is not supported....的相关文章

json-lib-2.4-jdk15.jar 报错 net.sf.json.JSONException: There is a cycle in the hierarchy!错误解决方案(Hibernate)

使用hibernate容易出现该问题,主要是由于pojo类属性存在级联关系.比如说员工和部门,在员工表里面有部门属性,而在部门表里面有个员工集合,这样就存在了嵌套引用的问题了,就会抛出这个异常. 解决方法很简单,在将每个对象转为json对象的时候用setExcludes函数将级联的属性去除掉就可以了,如下面: 比如CityVo  里面有关联属性 ProvinceVo province 以及 地区属性 Set<DistrictVo> districtList 使用hibernate 查询所有的c

.NET Core 3.0 System.Text.Json 和 Newtonsoft.Json 行为不一致问题及解决办法

行为不一致 .NET Core 3.0 新出了个内置的 JSON 库, 全名叫做尼古拉斯 System.Text.Json - 性能更高占用内存更少这都不是事... 对我来说, 很多或大或小的项目能少个第三方依赖项, 还能规避多个依赖项的依赖 Newtonsoft.Json 版本不一致的问题, 是件极美的事情. 但是, 结果总不是不如预期那么简单和美好, 简单测试了下, 有一些跟 Newtonsoft.Json 行为不一致的地方, 代码如下: using Microsoft.VisualStud

.net core中关于System.Text.Json的使用

在.Net Framework的时候序列化经常使用Newtonsoft.Json插件来使用,而在.Net Core中自带了System.Text.Json,号称性能更好,今天抽空就来捣鼓一下. 使用起来其实也很简单,就是有些地方要注意,比如在我们的对象实体中有中文的话,直接序列化时中文会被转换成编码格式,时间格式序列化时会被转成默认的格式等. 下面就直接上Demo的代码了 using System; using System.Text.Encodings.Web; using System.Te

C# 调用win32 DLL报错 System.BadImageFormatException

C# 调用win32 DLL报错  System.BadImageFormatException 项目右键属性->项目设计器->生成->平台->把'默认设置(任何 CPU)'改为x86. 因为'任何 CPU'的程序在64位的机器上就会用运行为64位,而64程序是不能加载32位dll的

.NET下载文件报错System.UnauthorizedAccessException的解决方法

转自原文.NET下载文件报错System.UnauthorizedAccessException的解决方法 假设VS代码对应路径为E:\Projects\Web1,在VS用“发布Web”的方式发布后的路径为E:\Site\Web1.在IIS新建2个站点,站点A指向E:\Projects\Web1,站点B指向E:\Site\Web1. 现在出现一个异常情况,站点B能正常下载123.xls,站点A下载时却提示错误: System.UnauthorizedAccessException: 对路径“E:

查询数据库语句报错“数据类型 text 和 varchar 在 equal to 运算符中不兼容。&quot;

问题:查询数据库语句报错"数据类型 text 和 varchar 在 equal to 运算符中不兼容." 解决: select * from 表名称 where cast(查询的字段名称 as varchar(max)) ='查询的值'

ASP.NET MVC页面报错System.InvalidOperationException The view found at &#39;~/Views/Home/Index.cshtml&#39; was not created.

Application Exception System.InvalidOperationException The view found at '~/Views/Home/Index.cshtml' was not created. Description: HTTP 500.Error processing request. Details: Non-web exception. Exception origin (name of application or object): System

【IntellJ IDEA】idea启动测试类报错Error running &#39;Test1.test&#39;: Command line is too long. Shorten command line for Test1.test or also for JUnit default configuration.

idea启动测试类报错 Error running 'Test1.test': Command line is too long. Shorten command line for Test1.test or also for JUnit default configuration. 注意: 最简单的方法,就是你重新创建一个新的测试类,在里面重新写一遍测试方法,代码都可以粘贴过去. 解决方法: 1.打开本项目的.idea文件夹,找到文件夹中的workspace.xml文件 2.搜索 Proper

Net core 2.x 升级 3.0 使用自带 System.Text.Json 时区 踩坑经历

.Net Core 3.0 更新的东西很多,这里就不多做解释了,官方和博园大佬写得很详细 关于 Net Core 时区问题,在 2.1 版本的时候,因为用的是 Newtonsoft.Json,配置比较方便 AddJsonOptions(opt => { opt.SerializerSettings.DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Local; }) 但是用 System.Text.Json 就没那么方便了,翻