EF Code First 6使用“Multiple”多次自我引用

时间:2014-02-12 13:05:14

标签: entity-framework ef-code-first self-referencing-table

我有一个这样的实体:

public class Course
{
    public int Id { get; set; }
    public string Name { get; set; } 

    public virtual ICollection<Course> Prerequisites { get; set; }
    public virtual ICollection<Course> Equivalents { get; set; }

    public Course()
    {
        Prerequisites = new HashSet<Course>();
        Equivalents = new HashSet<Course>();
    }
}

我想为“先决条件”和“等价物”创建不同的表格。 我该如何配置呢?

1 个答案:

答案 0 :(得分:1)

这可能会有所帮助

public class Course
{

    public int Id { get; set; }
    public String Name { get; set; }

    [InverseProperty("PrerequisiteFor")]
    public virtual ICollection<Course> Prerequisites { get; set; }
    [InverseProperty("EquivalentTo")]
    public virtual ICollection<Course> Equivalents { get; set; }
    [InverseProperty("Equivalents")]
    public virtual ICollection<Course> EquivalentTo { get; set; }
    [InverseProperty("Prerequisites")]
    public virtual ICollection<Course> PrerequisiteFor { get; set; }

    public Course()
    {
        Prerequisites = new HashSet<Course>();
        Equivalents = new HashSet<Course>();
        PrerequisiteFor = new HashSet<Course>();
        EquivalentTo = new HashSet<Course>();

    }
}