// This function is used for the case which meet the following two condition: // 1) All pass-in parameters are in detached state // 2) DbContext.Configuration.LazyLoadingEnabled is true public void RemoveStudentFromClub(Student s, Club c) { //Type II Many-to-Many relationship, which has been broken into two one-to-many relationship StudentClubMatch scMatch = DbContext.StudentClubMatches.Single(o => o.ClubId == c.Id && o.StudentId == s.Id); // Remove student from a club by deleting the StudentClubMatch DbContext.Entry(scMatch).State = EntityState.Deleted; DbContext.SaveChanges(); }
// This function is used for the case which meet the following two condition: // 1) All pass-in parameters are in detached state // 2) DbContext.Configuration.LazyLoadingEnabled is true // Entity based version public void AddStudentToClubVersion1(Student s, Club c, bool isCommitteMember) { //Type II Many-to-Many relationship, which has been broken into two one-to-many relationship //Create and add the matching table record first. StudentClubMatch scMatch = new StudentClubMatch(); scMatch.StudentId = s.Id; scMatch.ClubId = c.Id; scMatch.IsCommiteMember = isCommitteMember; DbContext.Entry(scMatch).State = EntityState.Added; DbContext.SaveChanges(); }
// This function is used for the case which meet the following two condition: // 1) All pass-in parameters are in detached state // 2) DbContext.Configuration.LazyLoadingEnabled is true // Relationship based version public void AddStudentToClubVersion2(Student s, Club c, bool isCommitteMember) { //Type II Many-to-Many relationship, which has been broken into two one-to-many relationship //Create and add the matching table record first. StudentClubMatch scMatch = new StudentClubMatch(); scMatch.Student = s; s.StudentClubMatches.Add(scMatch); scMatch.Club = c; c.StudentClubMatches.Add(scMatch); scMatch.IsCommiteMember = isCommitteMember; DbContext.SaveChanges(); }
public void TestAddRemoveStudentFromClub() { //Add student to class for Version1 Student s = _demoLib.DbContext.Students.Single(o => o.Id == 1); Club c = _demoLib.DbContext.Clubs.Single(o => o.Id == 1); _demoLib.AddStudentToClubVersion1(s, c, false); StudentClubMatch scMatch = _demoLib.DbContext.StudentClubMatches.Single(o => o.ClubId == c.Id && o.StudentId == s.Id); Assert.AreSame(scMatch.Student, s); Assert.AreSame(scMatch.Club, c); Assert.IsTrue(s.StudentClubMatches.Contains(scMatch)); Assert.IsTrue(c.StudentClubMatches.Contains(scMatch)); Assert.IsTrue(_demoLib.DbContext.StudentClubMatches.ToList().Contains(scMatch)); //Add student to class for Version2 Student s2 = _demoLib.DbContext.Students.Single(o => o.Id == 2); Club c2 = _demoLib.DbContext.Clubs.Single(o => o.Id == 2); _demoLib.AddStudentToClubVersion2(s2, c2, false); StudentClubMatch scMatch2 = _demoLib.DbContext.StudentClubMatches.Single(o => o.ClubId == c2.Id && o.StudentId == s2.Id); Assert.AreSame(scMatch2.Student, s2); Assert.AreSame(scMatch2.Club, c2); Assert.IsTrue(s2.StudentClubMatches.Contains(scMatch2)); Assert.IsTrue(c2.StudentClubMatches.Contains(scMatch2)); Assert.IsTrue(_demoLib.DbContext.StudentClubMatches.ToList().Contains(scMatch2)); //Delete Student from class _demoLib.RemoveStudentFromClub(s, c); Assert.IsFalse(s.StudentClubMatches.Contains(scMatch)); Assert.IsFalse(c.StudentClubMatches.Contains(scMatch)); Assert.IsFalse(_demoLib.DbContext.StudentClubMatches.ToList().Contains(scMatch)); //Delete Student from class _demoLib.RemoveStudentFromClub(s2, c2); Assert.IsFalse(s.StudentClubMatches.Contains(scMatch2)); Assert.IsFalse(c.StudentClubMatches.Contains(scMatch2)); Assert.IsFalse(_demoLib.DbContext.StudentClubMatches.ToList().Contains(scMatch2)); }
// This function is used for the case which meet the following two condition: // 1) All pass-in parameters are in detached state // 2) DbContext.Configuration.LazyLoadingEnabled is true public void UpdateStudentClubMatch(StudentClubMatch match) { DbContext.Entry(match).State = EntityState.Modified; DbContext.SaveChanges(); }