【问题标题】:Self referencing one-to-many relationship in EF Core 5EF Core 5 中的自引用一对多关系
【发布时间】:2021-06-27 16:58:38
【问题描述】:

我正在尝试实现这个所需的架构:

富

FooId
1  
2 
3 

FooMapping

ParentFooId | ChildFooId
1             2
1             3

我想我需要这样的模型:

class Foo
{
    public int Id { get; set; }
    public Foo Parent { get; set; }
    public List<Foo> Children { get; set; }
}

还有这样的配置:

builder.Entity<Foo>().HasMany(c => c.Children).WithOne(c => c.Parent);

现在 EF Core 将采用上述方法并创建如下架构:

富

FooId | ParentId

我觉得从架构/查询的角度来看,一对多的映射表会更有意义,不是吗? EF Core 5 是如何实现的?

【问题讨论】:

  • 这只是一个 1:n 的关联,不需要联结表。模式和查询都没有意义。

标签: c# entity-framework entity-framework-core ef-core-5.0


【解决方案1】:

我觉得从架构/查询的角度来看,一对多的映射表会更有意义,不是吗?

好的。我觉得那只是一张额外的桌子。

EF Core 5 是如何实现的?

通过将 FooMapping 作为实体添加到模型中。

【讨论】:

    猜你喜欢
    • 2021-08-13
    • 1970-01-01
    • 1970-01-01
    • 2021-07-21
    • 2021-03-30
    • 1970-01-01
    • 2017-10-26
    • 1970-01-01
    • 2021-03-12
    相关资源
    最近更新 更多