In the following, between Students and People, a join table is created (“PersonsCourses”). The FKs are in the join table, and the Navigation properties are in their effective table. Left refers to the first (HasMany), Right refers to the second (WithMany).

Note that the HasMany indicates that a course has many students and WithMany indicates that a person can attend a lot of courses.

            HasMany(c => c.Students).
            WithMany(p => p.CoursesAttending).
            Map(
                m =>
                {
                    m.MapLeftKey("CourseId");
                    m.MapRightKey("PersonId");
                    m.ToTable("PersonCourses");
                });

Even if there is a convention, always use the Map method to explicitly define the table and column names, due to the following issue: http://stackoverflow.com/a/7966436/1052767

  • /home/skysigal/public_html/data/pages/it/ad/code_first/howto/define_models/relationships/examples/n-n.txt
  • Last modified: 2023/11/04 23:30
  • by 127.0.0.1