使用 EntityFramework.Core 从自引用表加载完整的层次结构 [英] loading a full hierarchy from a self referencing table with EntityFramework.Core

查看:33
本文介绍了使用 EntityFramework.Core 从自引用表加载完整的层次结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

解释为什么这个问题不同于:EF- 多个包含以急切加载分层数据.不好的做法?

Explanation why this question is different to: EF - multiple includes to eager load hierarchical data. Bad practice?

  1. 可能的重复是一个基于意见的问题,如果这是一个不好的做法,而我的问题往往是关于如何做到这一点的技术解决方案,独立于意见,如果它是一个好的做法与否.我将这个决定留给产品负责人、需求工程师、项目经理和需要该功能的客户.
  2. 给出的答案要么解释了为什么这是不好的做法,要么使用了对我不起作用的方法(使用 Include() 和 ThenInclude() 产生硬编码深度,而我需要灵活的深度).

<小时>

在当前项目(.NET core web api)中,我尝试从自引用表加载层次结构.


In the current project (a .NET core web api) I try to load a hierarchy from a self referencing table.

在谷歌搜索了很多之后,我很惊讶这样的任务(我认为这很微不足道)似乎并不微不足道.

After googling a lot I was surprised that such a task (which I thought would be trivial) seems not to be trivial.

好吧,我有这张表来形成我的层次结构:

Well, I have this table to form my hierarchy:


CREATE TABLE [dbo].[Hierarchy] (
    [Id]        INT           IDENTITY (1, 1) NOT NULL,
    [Parent_Id] INT           NULL,
    [Name]      NVARCHAR (50) NOT NULL,
    PRIMARY KEY CLUSTERED ([Id] ASC),
    CONSTRAINT [FK_Hierarchy_Hierarchy] FOREIGN KEY ([Parent_Id]) REFERENCES [dbo].[Hierarchy] ([Id])
);

在 web api 中,我尝试返回完整的层次结构.一件可能特别的事情(可能会有所帮助)是我想加载完整的表格.

In the web api I try to return the complete hierarchy. One maybe special thing (that could help) is the fact that I want to load the complete table.

我也知道我可以使用预先加载和导航属性(Parent 和 InverseParent 用于孩子)

I also know that I could use eager loading and the navigation property (Parent and InverseParent for children)


_dbContext.Hierarchy.Include(h => h.InverseParent).ThenInclude(h => h.InverseParent)...

这样做的问题是,这会加载一个硬编码深度(例如,如果我使用 1 个 Include() 和 5 个 ThenInclude(),则为六个级别),但我的层次结构具有灵活的深度.

The problem with that is that this would load a hard coded depth (e.g. six levels if I use 1 Include() and 5 ThenInclude()) but my hierarchy has a flexible depth.

任何人都可以通过给我一些代码来帮助我如何加载完整表(例如,在具有 1 个 DB 调用的最佳场景中加载到内存中),然后使该方法返回完整的层次结构?

Can anyone help me out by giving me some code how to load the full table (e.g. into memory in an optimal scenario with 1 DB call) and then make the method return the full hierarchy?

推荐答案

事实上,由于所谓的 EF (Core) 关系修复整个层次结构非常容易>.

In fact loading the whole hierarchy is quite easy thanks to the so called EF (Core) relationship fixup.

假设我们有以下模型:

public class Hierarchy
{
    public int Id { get; set; }
    public string Name { get; set; }
    public Hierarchy Parent { get; set; }
    public ICollection<Hierarchy> Children { get; set; }
}

然后是下面的代码

var hierarchy = db.Hierarchy.Include(e => e.Children).ToList();

将使用正确填充的 ParentChildren 属性加载整个层次结构.

will load the whole hierarchy with correctly populated Parent and Children properties.

当您只需要加载层次结构的一部分时,引用的帖子中描述的问题就会出现,这很困难,因为 LINQ 中缺乏类似 CTE 的支持.

The problem described in the referenced posts arise when you need to load just part of the hierarchy, which is hard due to the lack of CTE like support in LINQ.

这篇关于使用 EntityFramework.Core 从自引用表加载完整的层次结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆