使用联结 table 和 EF 中的主键在同一 table 上建立多对多关系

Many-to-many relationship on the same table using a junction table and a primary key in EF

我有以下 tables:

我希望能够通过 EF 访问与主要子选项关联的所有次要子选项,反之亦然。直接使用 .Map 将不起作用,因为联结点 table Sub_Option_To_Sub_Option 具有主键。

public class Sub_Option
{
    public int Sub_Option_ID { get; set; }
    public string Name { get; set; }
}

对应Table

CREATE TABLE Sub_Option(
Sub_Option_ID int,
Name varchar(255)
);

和Table

CREATE TABLE Sub_Option_To_Sub_Option(
Sub_Option_To_Sub_Option int PK, 
Sub_Option_ID_Primary int,
Sub_Option_ID_Secondary int
);

我认为这应该有效:

    public class OptionToOption
{
    [Key]
    public int ID { get; set; }

    [ForeignKey("PrimaryOption")]
    public int PrimaryID { get; set; }

    [ForeignKey("SecondaryOption")]
    public int SecondaryID { get; set; }

    public virtual Option PrimaryOption { get; set; }

    public virtual Option SecondaryOption { get; set; }
}

    public class Option
{
    public Option()
    {
        OptionToOption = new HashSet<OptionToOption>();
    }

    [Key]
    public int ID { get; set; }

    public string Name { get; set; }

    public virtual ICollection<OptionToOption> OptionToOption { get; set; }
}

并且在流利的 api 地图中是这样的(甚至不认为有必要这样做):

            modelBuilder.Entity<Option>()
            .HasMany(e => e.OptionToOption)
            .WithRequired(e => e.PrimaryOption)
            .HasForeignKey(e => e.PrimaryID);

        modelBuilder.Entity<Option>()
            .HasMany(e => e.OptionToOption)
            .WithRequired(e => e.SecondaryOption)
            .HasForeignKey(e => e.SecondaryID);