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

Posted 叶动风随

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ef core 自引用类 报错System.Text.Json.JsonException: A possible object cycle was detected which is not sup相关的知识,希望对你有一定的参考价值。

 

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

 

以上是关于ef core 自引用类 报错System.Text.Json.JsonException: A possible object cycle was detected which is not sup的主要内容,如果未能解决你的问题,请参考以下文章

将类别父 ID 自引用表结构映射到 EF Core 实体

EF Core 2.1:具有一对多关系的自引用实体生成附加列

1搭建DDD架构+EF

EF Core 删除对自身的引用而不删除

EF Core codefirst数据迁移

使用 EntityFramework.Core 从自引用表加载完整的层次结构