该模型具有项目实体。并且会有一些项目依赖于(使用)其他项目。多对多关系。例子:
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多对多关系:
尚不支持没有实体类来表示联接表的多对多关系。但是,可以通过为联接表包括一个实体类并映射两个单独的一对多关系来表示多对多关系。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句