public void MergeChanges_Should_Merge_Updates_For_Unchanged_Employee_With_Changed_Territories() { // Arrange var database = new MockNorthwind(); var employee = database.Employees[0]; var unchangedTerritory = employee.Territories[0]; var modifiedTerritory = employee.Territories[1]; var deletedTerritory = employee.Territories[2]; var addedExistingTerritory = database.Territories[3]; var area1 = new Area { AreaId = 1, AreaName = "Northern", Territories = new ChangeTrackingCollection<Territory> { unchangedTerritory } }; var area2 = new Area { AreaId = 2, AreaName = "Southern", Territories = new ChangeTrackingCollection<Territory> { modifiedTerritory } }; var area3 = new Area { AreaId = 3, AreaName = "Eastern", Territories = new ChangeTrackingCollection<Territory> { deletedTerritory } }; var area4 = new Area { AreaId = 4, AreaName = "Western", Territories = new ChangeTrackingCollection<Territory> { addedExistingTerritory } }; var addedNewTerritory = new Territory { TerritoryId = "91360", TerritoryDescription = "SouthernCalifornia", Employees = new ChangeTrackingCollection<Employee> { employee }, AreaId = 5, TrackingState = TrackingState.Added // Must explicitly mark as added }; unchangedTerritory.Area = area1; unchangedTerritory.AreaId = area1.AreaId; modifiedTerritory.Area = area2; modifiedTerritory.AreaId = area2.AreaId; deletedTerritory.Area = area3; deletedTerritory.AreaId = area3.AreaId; addedExistingTerritory.Area = area4; addedExistingTerritory.AreaId = area4.AreaId; var changeTracker = new ChangeTrackingCollection<Employee>(employee); modifiedTerritory.TerritoryDescription = "xxx"; // mod prop modifiedTerritory.AreaId = 6; // mod FK prop employee.Territories.Remove(deletedTerritory); // should mark area deleted employee.Territories.Add(addedExistingTerritory); // should keep area unchanged employee.Territories.Add(addedNewTerritory); // should keep territory added addedExistingTerritory.Area.AreaName = "zzz"; // should mark area modified var changes = changeTracker.GetChanges(); // should exclude unchanged territory var updatedEmployee = UpdateEmployeesWithTerritories(changes)[0]; // rel entities, db-gen vals // Act changeTracker.MergeChanges(updatedEmployee); // Assert Assert.Contains(unchangedTerritory, employee.Territories); // Unchanged present Assert.Equal("zzz", addedExistingTerritory.Area.AreaName); // Area name updated Assert.Equal("yyy", addedNewTerritory.Data); // Db-generated value set Assert.Equal(addedNewTerritory.AreaId, addedNewTerritory.Area.AreaId); // Added territory Area set Assert.Equal(modifiedTerritory.AreaId, modifiedTerritory.Area.AreaId); // Modified territory Area set Assert.Same(addedExistingTerritory, employee.Territories.Single(t => t.TerritoryId == "02116")); // Ref equality Assert.Same(addedNewTerritory, employee.Territories.Single(t => t.TerritoryId == "91360")); // Ref equality Assert.Same(modifiedTerritory, employee.Territories.Single(t => t.TerritoryId == "01730")); // Ref equality Assert.DoesNotContain(deletedTerritory, employee.Territories); // Detail deleted ICollection cachedDeletes = ((ITrackingCollection)employee.Territories).GetChanges(true); Assert.Empty(cachedDeletes); // Cached deletes have been removed }
public void MergeChanges_Should_Set_Modified_Employee_With_Unchanged_Territories() { // Arrange var database = new MockNorthwind(); var employee = database.Employees[0]; var unchangedterritory = employee.Territories[0]; var modifiedterritory = employee.Territories[1]; var deletedterritory = employee.Territories[2]; var addedExistingTerritory = database.Territories[3]; var addedNewTerritory = new Territory { TerritoryId = "91360", TerritoryDescription = "SouthernCalifornia", Employees = new ChangeTrackingCollection<Employee> {employee} }; var changeTracker = new ChangeTrackingCollection<Employee>(employee); var name = employee.LastName = "xxx"; var origState = employee.TrackingState; var changes = changeTracker.GetChanges(); var changedTerritories = changes[0].Territories; var updatedEmployee = changes[0].Clone<Employee>(); updatedEmployee.AcceptChanges(); // Act changeTracker.MergeChanges(updatedEmployee); // Assert Assert.Equal(name, employee.LastName); // Changed prop is preserved Assert.Empty(changedTerritories); // GetChanges sets unchanges ref props to null Assert.Equal(3, employee.Territories.Count); // Unchanged items preserved }