简体   繁体   English

定义引用同一个表的多对多关系(EF7/核心)

[英]Defining a many-to-many relationship referencing the same table (EF7/core)

The model has item entities.该模型具有项目实体。 And there will be items that are dependent on ( using ) other items.并且会有依赖(使用)其他项目的项目。 Many-to-many relationship.多对多关系。 Example:例子:

Item A is used by Item B, C, and F.
Item B is used by Item C, F and H.

How one would correctly define directional relationships between different items?如何正确定义不同项目之间的方向关系?

The Item:项目:

public class Item
    {
    public int Id
        { get; set;}
    public string Name
        {get; set;}
    }

My first approach to define the dependencies would be:我定义依赖项的第一种方法是:

public class ItemDependency
{
    [Key]
    public int Id
    { get; set; }

    [ForeignKey("ItemParentId")]
    public Item ItemParent { get; set; }

    public int ItemParentId{ get; set; }

    [ForeignKey("ItemDependentId")]
    public Item ItemDependentId { get; set; }

    public int ItemDependentId { get; set; }

}

According to the documentation EF7 Many-to-many relationship :根据文档EF7 多对多关系

Many-to-many relationships without an entity class to represent the join table are not yet supported.尚不支持没有实体类来表示连接表的多对多关系。 However, you can represent a many-to-many relationship by including an entity class for the join table and mapping two separate one-to-many relationships.但是,您可以通过为连接表包含一个实体类并映射两个单独的一对多关系来表示多对多关系。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 EF Core-如何为已经存在一对多关系的同一实体设置多对多 - EF Core - How to setup many-to-many for same entity that already has a one-to-many relationship 使用EF到同一表的多对多关系 - Many-to-Many Relationships to Same Table with EF 如何使用自动映射器将具有多对多关系的 DTO 映射到具有关系表的 EF 核心实体? - How to map a DTO with a many-to-many relationship to a EF core entity with a relationship table using automapper? EF Core 中的多对多关系,依赖实体和主体实体都是同一类型 - Many-to-Many relationship in EF Core with dependent and principal entity both being the same type C#Core 2.1 EF多对多关系查询 - C# Core 2.1 EF many-to-many relationship querying 如何修改EF Core 5中多对多实体关系中生成的连接表 - How to modify the join table generated in many-to-many entity relationship in EF Core 5 如何直接填充 EF Core 为多对多关系生成的连接表? - How can I directly fill join table generated by EF Core for many-to-many relationship? EF Core 3.1 如何在多对多关系中自动映射 - EF Core 3.1 how to automap in Many-to-many relationship EF Core如何选择具有多对多关系的实体 - EF Core how select entity with many-to-many relationship EF核心多对多关系身份插入错误 - EF Core Many-to-Many Relationship Identity Insert Error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM