Evening, I have recently gotten help making my database in MVC on SO (I'm very thankful for that). I have another question, as I'm not in graduate school yet so I don't know the best-practice, I thought someone might in my case.
I am making joint tables between two classes, a User
table and a Course
table. Put simply, a User can have a Course, and this is being made in my OnModelCreating
method of my DbContext class
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new UserMap());
base.OnModelCreating(modelBuilder);
}
<-- snip --> Below is my class for the mappings <-- snip -->
public class UserMap : EntityTypeConfiguration<ApplicationUser>
{
public UserMap()
{
// Primary Key
this.HasKey(m => m.Id);
// UserHasCourse
this.HasMany(m => m.Courses)
.WithMany()
.Map(n =>
{
n.MapLeftKey("UserId");
n.MapRightKey("CourseId");
n.ToTable("UserHasCourse");
});
}
}
What I want to do is be able to add more columns to the UserHasCourse
. The question is, how do I do this if the table is being generated here? This will not be the only case where I need to add a column/s that isn't necessarily related to the model (ie. I want to add the columns Credits
and Grade
, corresponding to the number of credits a user has earned in the course with a grade). I don't want to have Credits
and Grade
saved in the Courses
table (as they only need to be in this joint table).
I can always add in two columns in the Server Explorer, but is there a way I should do this in code (if it is better that way)?