定义引用相同 table (EF7/core) 的多对多关系
Defining a many-to-many relationship referencing the same table (EF7/core)
模型有 item 个实体。
并且会有依赖于(using)其他项目的项目。多对多关系。示例:
Item A is used by Item B, C, and F.
Item B is used by Item C, F and H.
如何正确定义不同项目之间的方向关系?
项目:
public class Item
{
public int Id
{ get; set;}
public string Name
{get; set;}
}
我定义依赖关系的第一种方法是:
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; }
}
根据文档EF7 Many-to-many relationship:
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.
模型有 item 个实体。 并且会有依赖于(using)其他项目的项目。多对多关系。示例:
Item A is used by Item B, C, and F.
Item B is used by Item C, F and H.
如何正确定义不同项目之间的方向关系?
项目:
public class Item
{
public int Id
{ get; set;}
public string Name
{get; set;}
}
我定义依赖关系的第一种方法是:
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; }
}
根据文档EF7 Many-to-many relationship:
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.