This excerpt code successfully creates a many-many relationship with an explicit Junction table that has additional data within it.
PROBLEM:
public class Student
{
public virtual int StudentId { get; set; }
public virtual string StudentName { get; set; }
public virtual ICollection Enrollments { get; set; }
}
public class Course
{
public virtual int CourseId { get; set; }
public virtual string CourseName { get; set; }
public virtual ICollection Enrollments { get; set; }
}
public class Enrollment
{
public virtual int StudentId { get; set; }
public virtual int CourseId { get; set; }
public virtual string Grade { get; set; }
public virtual Student Student { get; set; }
public virtual Course Course { get; set; }
}
public class ManyMany : DbContext
{
public DbSet Students { get; set; }
public DbSet Courses { get; set; }
public DbSet Enrollments { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity()
.HasKey(student => student.StudentId);
modelBuilder.Entity()
.HasKey(course => course.CourseId);
modelBuilder.Entity()
.HasKey(enrollment => new { enrollment.StudentId, enrollment.CourseId } );
modelBuilder.Entity()
.HasMany(student => student.Enrollments)
.WithRequired(enrollment => enrollment.Student)
.HasForeignKey(enrollment => enrollment.StudentId);
modelBuilder.Entity()
.HasMany(course => course.Enrollments)
.WithRequired(enrollment => enrollment.Course)
.HasForeignKey(enrollment => enrollment.CourseId);
}
}
Older question concerning this, answer and more info here: Entity Framework CodeFirst many to many relationship with additional information.
EDIT: Usage example:
var context = new ManyMany();
var physicsCourse = new Course() { CourseName = "Physics" };
var mathCourse = new Course() { CourseName = "Math" };
var studentJohn = new Student() { StudentName = "John Doe" };
var studentJane = new Student() { StudentName = "Jane Doe" };
var physicsCourseEnrollmentJohn = new Enrollment() { Student = studentJohn, Course = physicsCourse };
var mathCourseEnrollmentJohn = new Enrollment() { Student = studentJohn, Course = mathCourse };
var physicsCourseEnrollmentJane = new Enrollment() { Student = studentJane, Course = physicsCourse };
context.Courses.Add(physicsCourse);
context.Courses.Add(mathCourse);
context.Students.Add(studentJohn);
context.Students.Add(studentJane);
studentJohn.Enrollments.Add(physicsCourseEnrollmentJohn);
studentJohn.Enrollments.Add(mathCourseEnrollmentJohn);
studentJane.Enrollments.Add(physicsCourseEnrollmentJane);
physicsCourse.Enrollments.Add(physicsCourseEnrollmentJohn);
mathCourse.Enrollments.Add(mathCourseEnrollmentJohn);
physicsCourse.Enrollments.Add(physicsCourseEnrollmentJane);
context.Enrollments.Add(physicsCourseEnrollmentJohn);
context.Enrollments.Add(mathCourseEnrollmentJohn);
context.Enrollments.Add(physicsCourseEnrollmentJane);
context.SaveChanges();
var johnsEnrollments = context.Students.Where(student => student.StudentId == studentJohn.StudentId).Single().Enrollments;
MessageBox.Show(string.Format("Student John has enrolled in {0} courses.", johnsEnrollments.Count));
var janesEnrollments = context.Students.Where(student => student.StudentId == studentJane.StudentId).Single().Enrollments;
MessageBox.Show(string.Format("Student Jane has enrolled in {0} courses.", janesEnrollments.Count));