Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Entity framework 首先在EF代码中表示图_Entity Framework_Ef Code First_Code First - Fatal编程技术网

Entity framework 首先在EF代码中表示图

Entity framework 首先在EF代码中表示图,entity-framework,ef-code-first,code-first,Entity Framework,Ef Code First,Code First,我试图在实体框架代码优先模型中用类型化边表示一个图。我很难理解如何正确地建立关系。我将图形中的节点称为“项”,边称为“关系”,这就是我所拥有的: public class Item : Learnable { public Boolean IsBeginningItem { get; set; } public virtual List<Relationship> RelationshipsLeft { get; set; } public virtual L

我试图在实体框架代码优先模型中用类型化边表示一个图。我很难理解如何正确地建立关系。我将图形中的节点称为“项”,边称为“关系”,这就是我所拥有的:

public class Item : Learnable
{
    public Boolean IsBeginningItem { get; set; }
    public virtual List<Relationship> RelationshipsLeft { get; set; }
    public virtual List<Relationship> RelationshipsRight { get; set; }
}
下面是我得到的:

如何使Item的RelationshipsRight属性与Relationship的ItemLeft属性相对应,以及如何使Item的RelationshipsLeft属性与Relationship的ItemRight属性相对应


哦。。。我想我应该解释一下,这应该是一个有向图,我可以双向导航

您可以使用
[InverseProperty]
属性将正确的导航属性对绑定在一起:

public class Relationship
{
    //...

    public int ItemLeftID { get; set; }
    [ForeignKey("ItemLeftID"), InverseProperty("RelationshipsRight")]
    public virtual Item ItemLeft { get; set; }

    public int ItemRightID { get; set; }
    [ForeignKey("ItemRightID"), InverseProperty("RelationshipsLeft")]
    public virtual Item ItemRight { get; set; }
}

哇,这么简单,是吗?非常感谢,它工作得非常好!
public class Relationship
{
    //...

    public int ItemLeftID { get; set; }
    [ForeignKey("ItemLeftID"), InverseProperty("RelationshipsRight")]
    public virtual Item ItemLeft { get; set; }

    public int ItemRightID { get; set; }
    [ForeignKey("ItemRightID"), InverseProperty("RelationshipsLeft")]
    public virtual Item ItemRight { get; set; }
}