Reputation: 17298
i have 3 table. 2 of them has got many to many relations. Last one is secondary table like that:
Student:
id------- Name------- SurName
1-------- fsfsf---------- fsfsfsdf
2-------- dfdsf -------- sfsfsdfsdf
Course
id----------Name-------
11-------- course1----------
22-------- course2--------
23-------- course3--------
StudentCourse
Studentid---------CourseId
1-------------------11
1-------------------12
2-------------------22
2-------------------23
2-------------------11
But Secondary table is hidden inside of the Entity framework model. But i need to add Studentid and Courseid without any changes of Course and Student table. How can i achive that?
Upvotes: 0
Views: 137
Reputation: 165
You can use Fluent-API to map your relation table. To achieve that, you have to edit the OnModelCreating method like this:
modelBuilder.Entity<Course>()
.HasMany<Student>(c => c.Students)
.WithMany(s => s.Courses)
.Map(m => {
m.ToTable("StudentCourses");
m.MapLeftKey("CourseId");
m.MapRightKey("StudentId");
});
Don't forget to do an "update-database" command on your Package Manager.
Upvotes: 1