【问题标题】:EF Code First 6 With "Multiple" many to many self referencingEF Code First 6 带有“多个”多对多自引用
【发布时间】:2014-02-12 13:05:13
【问题描述】:

我有一个这样的实体:

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

我想为“先决条件”和“等效项”创建不同的表。 如何配置?

【问题讨论】:

  • 那你为什么要把它们做成相同类型的课程呢?如果您希望它们在不同的表中,我认为您遇到了设计问题。
  • 感谢您的回复。你能写出你的建议吗?

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


【解决方案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>();

    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-11
    • 1970-01-01
    • 1970-01-01
    • 2011-09-07
    • 2013-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多