Just as an example I'm using the code from this article on codeproject: http://www.codeproject.com/Articles/234606/Creating-a-Many-To-Many-Mapping-Using-Code-First
Im trying to do something similar in my code but I also want a quantity property, so:
I'm thinking I have to add a column in PersonCourses table named Quantity but I have no idea how to do that 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");
});
}
}
To add a column to the junction table, you need to map it as an entity explicitly and create two one-to-many relationships:
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>();
}
}
If you want to use Fluent Api instead Data Annotations, you can use these configurations:
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With