【问题标题】:Defining a many-to-many relationship referencing the same table (EF7/core)定义引用同一个表的多对多关系(EF7/core)
【发布时间】:2016-03-24 10:12:56
【问题描述】:

模型具有 item 实体。 并且会有一些项目依赖于(使用)其他项目。多对多的关系。示例:

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; }

}

【问题讨论】:

    标签: c# .net entity-framework many-to-many entity-framework-core


    【解决方案1】:

    根据文档EF7 Many-to-many relationship

    尚不支持没有实体类来表示连接表的多对多关系。但是,您可以通过包含连接表的实体类并映射两个单独的一对多关系来表示多对多关系。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-11-26
      • 2019-01-04
      • 1970-01-01
      • 2013-01-18
      • 1970-01-01
      • 1970-01-01
      • 2016-11-21
      相关资源
      最近更新 更多