[英]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.