ILIA hu
ILIA hu

Reputation: 157

EF Code First 6 With "Multiple" many to many self referencing

I have one entity like this:

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

I want to create to different table for "Prerequisites" and "Equivalents". How can i configure it?

Upvotes: 0

Views: 342

Answers (1)

Build80
Build80

Reputation: 11

This could help

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

    }
}

Upvotes: 1

Related Questions