1
我試圖在EF Code-first中實現多對多。我發現這個代碼:EF中的HashSet多對多
public class Student
{
public Student() { }
public int StudentId { get; set; }
[Required]
public string StudentName { get; set; }
public virtual ICollection<Course> Courses { get; set; }
}
public class Course
{
public Course()
{
this.Students = new HashSet<Student>();
}
public int CourseId { get; set; }
public string CourseName { get; set; }
public virtual ICollection<Student> Students { get; set; }
}
我明白,只是一切:
public Course()
{
this.Students = new HashSet<Student>();
}
你能告訴我,爲什麼這部分是必要的嗎?謝謝。