❔ Entity Framework - paired tables. Can add but not remove from paired table?
I've setup a Student class and a Course class. I've added the properties and also added necessary dbset in the dbcontext class.
I've added two students manually, one with id 1 and another with id 2 of course.
Same goes with courses.
(dbc = dbcontext)
I then add a course to student 2 with:
Student student = dbC.Students.First(s => s.StudentID == 2);
Course course = dbC.Courses.First(s => s.CourseId == 2);
student.Course.Add(course);
This will add to the the CourseStudent table:
CorseId: 2 | StudentStudentID: 2
(note: Why does it name the column to StudentStudentID and not the same with CorseId?)
If I repeat the code above but change from Add to Remove, it does not remove it. I've added a breakpoint and if I hove above course variable it will say that "Students" is null.
Where did things go wrong? 😅
3 Replies