1

as you know we don't have automatic Many to Many Relations between Entities in EF Core. whats the best solution to achieve that? here what I create for do that:

 class Student
{
    public int StudentId { get; set; }
    public string Name { get; set; }
    public string Family { get; set; }
    public List<StudentCourse> Courses  { get; set; }

}
class Course
{
    public int CourseId { get; set; }
    public string Name { get; set; }
    public List<Student> Students { get; set; }
}
class StudentCourse
{
    public int StudentCourseId { get; set; }
    public Student Student { get; set; }
    public Course Course { get; set; }
}
Rebwar
  • 413
  • 3
  • 17
  • 1
    Just one random duplicate. There are more candidates. – Gert Arnold Apr 02 '20 at 13:46
  • Not sure what's the question. If you want to achieve "automatic" many-to-many, apparently you can't since EFC does not support it yet. And all needed for achieving many-to-many with explicit join entity is in [EF Core documentation](https://learn.microsoft.com/en-us/ef/core/modeling/relationships?tabs=fluent-api%2Cfluent-api-simple-key%2Csimple-key#many-to-many). – Ivan Stoev Apr 02 '20 at 13:47
  • I know how to implement that, I say maybe there is a better solution than mine. – Rebwar Apr 02 '20 at 13:51
  • 1
    Better than the documentation? – Gert Arnold Apr 03 '20 at 07:09

1 Answers1

1

In your example you're doing it correctly, but I would say that Course entity should also have a List<StudentCourse> rather than List<Student to keep it consistent. Also add flat CourseId to the StudentCourse entity to control the foreign key in mapping configuration.

Using a join table is a common practice, and lack of the "automatic" many-to-many relations in EF Core forces you to define those mappings manually.

Personally I like that there is no automatic way in EF Core, because it gives more control and tidiness of table structure (like naming of the join table, forcing composite primary key etc.)

        public void Configure(EntityTypeBuilder<StudentCourse> builder)
        {
            builder.ToTable("StudentCourses");

            builder.HasKey(sc => new { sc.StudentId, sc.CourseId });  

            builder.HasOne(sc => sc.Student)
                .WithMany(s => s.StudentCourses)
                .HasForeignKey(sc => sc.StudentId);  

            builder.HasOne(sc => sc.Course)
                .WithMany(c => c.StudentCourses)
                .HasForeignKey(sc => sc.CourseId);
        }

    public class Student
    {
        public int StudentId { get; set; }

        public List<StudentCourse> StudentCourses { get; set; }
    }

    public class Course
    {
        public int CourseId { get; set; }

        public List<StudentCourse> StudentCourses { get; set; }
    }

    public class StudentCourse
    {
        public int StudentId { get; set; }

        public Student Student { get; set; }

        public int CourseId { get; set; }

        public Course Course { get; set; }
    }
rsobon
  • 1,012
  • 2
  • 12
  • 26