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