I have Person and Course Entities With multiple relationships:
public class Course
{
public int CourseID { get; set; }
public virtual Person Leader { get; set; }
public virtual ICollection<Person> Teachers { get; set; }
public virtual Person CreatedBy { get; set; }
public virtual Person UpdatedBy { get; set; }
...
}
public class Person
{
public int PersonID { get; set; }
public virtual ICollection<Course> CoursesCreated { get; set; }
public virtual ICollection<Course> CoursesUpdated { get; set; }
public virtual ICollection<Course> CoursesLead { get; set; }
public virtual ICollection<Course> CoursesTutored { get; set; }
...
}
I have the following mapping:
modelBuilder.Entity<Course>().HasMany(x => x.Tutors)
.WithMany(p => p.CoursesTutored)
.Map(x =>
{
x.MapLeftKey("PersonID");
x.MapRightKey("CourseID");
x.ToTable("Course_Tutors");
});
This works in that it sets up the Course_Tutors table as well as providing the course table with Leader_PersonID, CreatedBy_PersonID and UpdatedBy_PersonID.
However it also creates three extra Person columns in the course table: Person_PersonID1, Person_PersonID2, Person_PersonID3.
Can someone tell me how to get rid of these? I am assuming my mapping needs some more work.
Code First has problems identifying relationships by conversions when you have more than one relationship between two entities. You need to explicitly configure them.