C# 首先通过代码公开联接表的多对多

C# 首先通过代码公开联接表的多对多,c#,entity-framework,ef-code-first,many-to-many,C#,Entity Framework,Ef Code First,Many To Many,以下是Microsoft Access数据库中的关系图: 请注意,只有一个“节点”表。“节点_1”不是一个单独的表。当一个表的多个实例位于关系图中时,Access使用该命名约定 我想首先使用实体框架代码来定义一个类似的数据库 我已经开始草拟代码: namespace Relationships { public class Node { public int ID { get; set; } public string Title { get;

以下是Microsoft Access数据库中的关系图:

请注意,只有一个“节点”表。“节点_1”不是一个单独的表。当一个表的多个实例位于关系图中时,Access使用该命名约定

我想首先使用实体框架代码来定义一个类似的数据库

我已经开始草拟代码:

namespace Relationships
{
    public class Node
    {
        public int ID { get; set; }
        public string Title { get; set; }

        public virtual ObservableCollection<Relationship> Outgoing { get; set; }
        public virtual ObservableCollection<Relationship> Incoming { get; set; }
    }

    public class Label
    {
        public int ID { get; set; }
        public string Title { get; set; }
    }

    public class Relationship
    {
        public int ID { get; set; }
        public Node A { get; set; }
        public Node B { get; set; }
        public Label Label { get; set; }
    }

    public class Context : DbContext
    {
        public DbSet<Node> Nodes { get; set; }
        public DbSet<Label> Labels { get; set; }
        public DbSet<Relationship> Relationships { get; set; }
    }
}
命名空间关系
{
公共类节点
{
公共int ID{get;set;}
公共字符串标题{get;set;}
公共虚拟ObservableCollection传出{get;set;}
公共虚拟ObservableCollection传入{get;set;}
}
公共类标签
{
公共int ID{get;set;}
公共字符串标题{get;set;}
}
公共阶级关系
{
公共int ID{get;set;}
公共节点A{get;set;}
公共节点B{get;set;}
公共标签{get;set;}
}
公共类上下文:DbContext
{
公共数据库集节点{get;set;}
公共数据库集标签{get;set;}
公共数据库集关系{get;set;}
}
}
给定
节点的
传出
应返回该节点为
a
的关系<给定
节点的code>传入
应返回该节点为
B
的关系


设置此设置的好方法是什么?

传出的
传入的
中添加
反向属性
注释似乎达到了以下目的:

public class Node
{
    public int ID { get; set; }
    public string Title { get; set; }

    [InverseProperty("A")]
    public virtual ObservableCollection<Relationship> Outgoing { get; set; }
    [InverseProperty("B")]
    public virtual ObservableCollection<Relationship> Incoming { get; set; }
}
公共类节点
{
公共int ID{get;set;}
公共字符串标题{get;set;}
[逆属性(“A”)]
公共虚拟ObservableCollection传出{get;set;}
[反向属性(“B”)]
公共虚拟ObservableCollection传入{get;set;}
}