Adding a column in Entity Framework 6.1 - Lots to many - code first

As an example, I use the code from this article about codeproject: http://www.codeproject.com/Articles/234606/Creating-a-Many-To-Many-Mapping-Using-Code-First

enter image description here

I am trying to do something similar in my code, but also want a quantity property, therefore:

  • There can be many people in one course.
  • One person can have many courses.
  • Each person can choose the amount of each course. (I know that it makes no sense to choose the same course twice, but this is just an example, or replace courses with types of hamburgers :-))

I think I need to add a column in the PersonCourses table named Quantity, but I don't know how to do this in Code First.

The code:

public class Person
{
  public int PersonId { get; set; }
  public string FirstName { get; set; }
  public string LastName { get; set; }

  public ICollection<Course> CoursesAttending { get; set; }

  public Person()
  {
    CoursesAttending = new HashSet<Course>();
  }
}

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

  public ICollection<Person> Students { get; set; }

  public Course()
  {
    Students = new HashSet<Person>();
  }
}

public class SchoolContext : DbContext
{
  public DbSet<Course> Courses { get; set; }
  public DbSet<Person> People { get; set; }

  public SchoolContext()
    : base("MyDb")
  {
  }
}

Context:

public class SchoolContext : DbContext
{
  public DbSet<Course> Courses { get; set; }
  public DbSet<Person> People { get; set; }

  public SchoolContext()
    : base("MyDb")
  {
  }

  protected override void OnModelCreating(DbModelBuilder modelBuilder)
  {
    modelBuilder.Entity<Course>().
      HasMany(c => c.Students).
      WithMany(p => p.CoursesAttending).
      Map(
       m =>
       {
         m.MapLeftKey("CourseId");
         m.MapRightKey("PersonId");
         m.ToTable("PersonCourses");
       });
  }
}
+4
2

, --:

public class PersonCourse
{
   [Key, Column(Order = 0),ForeignKey("Person")]
   public int PersonId { get; set; }
   [Key, Column(Order = 1),ForeignKey("Course")]
   public int CourseId { get; set; }

   public Person Person { get; set; }
   public Course Course { get; set; }

   public int Quantity{ get; set; }
}

public class Person
{
  public int PersonId { get; set; }
  //...

  public ICollection<PersonCourse> CoursesAttending { get; set; }

  public Person()
  {
    CoursesAttending = new HashSet<PersonCourse>();
  }
}

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

  public ICollection<PersonCourse> Students { get; set; }

  public Course()
  {
    Students = new HashSet<PersonCourse>();
  }
}

Fluent Api Data Annotations, :

modelBuilder.Entity<PersonCourse>().HasKey(pc => new { pc.PersonId, pc.CourseId});

modelBuilder.Entity<PersonCourse>().HasRequired(pc=>pc.Person).WithMany(p=>p.CoursesAttending).HasForeignKey(pc=>pc.PersonId);

modelBuilder.Entity<PersonCourse>().HasRequired(pc=>pc.Course).WithMany(c=>c.Students).HasForeignKey(pc=>pc.CourseId);
+4

AS , PersonCourses, EF many-to-many (EF ). PersonCourses, EF many-to-many, --.

:

public class Person
{
  public int PersonId { get; set; }
  public string FirstName { get; set; }
  public string LastName { get; set; }

  public ICollection<PersonCourse> PersonCourses { get; set; }
}    
public class Course
{
  public int CourseId { get; set; }
  public string Title { get; set; }

  public ICollection<PersonCourse> PersonCoursess { get; set; }
}
public class PersonCourse {
    [Key, Column(Order = 0)]
    public int PersonId { get; set; }
    [Key, Column(Order = 1)]
    public int CourseId { get; set; }    
    public int Quantity { get; set; }
    public Person Person { get; set; }
    public Course Course { get; set; }
}
+2

All Articles