public void Apply_Changes_Should_Mark_Unchanged_Employee_As_Unchanged_And_Unchanged_Territories_With_Modified_Area_As_Modified()
        {
            // Ensure that changes are applied across M-M relationships.

            // Arrange
            var context = TestsHelper.CreateNorthwindDbContext(CreateNorthwindDbOptions);
            var nw = new MockNorthwind();
            var employee = nw.Employees[0];
            var territory1 = employee.Territories[0];
            var territory2 = employee.Territories[1];
            var territory3 = employee.Territories[2];
            var area = new Area
            {
                AreaId = 1,
                AreaName = "Northern",
                TrackingState = TrackingState.Modified
            };
            territory3.AreaId = 1;
            territory3.Area = area;

            // Act
            context.ApplyChanges(employee);

            // Assert
            Assert.AreEqual(EntityState.Unchanged, context.Entry(employee).State);
            Assert.AreEqual(EntityState.Unchanged, context.Entry(territory1).State);
            Assert.AreEqual(EntityState.Unchanged, context.Entry(territory2).State);
            Assert.AreEqual(EntityState.Unchanged, context.Entry(territory3).State);
            Assert.AreEqual(EntityState.Modified, context.Entry(area).State);
        }
        private List<Employee> CreateTestEmployees(NorthwindDbContext context)
        {
            // Create test entities
            var area1 = new Area { AreaName = "Northern" };
            var area2 = new Area { AreaName = "Southern" };
            var territory1 = context.Territories.Single(t => t.TerritoryId == TestTerritoryId1);
            var territory2 = context.Territories.Single(t => t.TerritoryId == TestTerritoryId2);
            var territory3 = context.Territories.Single(t => t.TerritoryId == TestTerritoryId3);
            territory1.Area = area1;
            territory2.Area = area2;
            territory3.Area = area2;
            var employee1 = new Employee
            {
                FirstName = "Test",
                LastName = "Employee1",
                City = "San Fransicso",
                Country = "USA",
                Territories = new List<Territory> { territory1, territory2 }
            };
            var employee2 = new Employee
            {
                FirstName = "Test",
                LastName = "Employee2",
                City = "Sacramento",
                Country = "USA",
                Territories = new List<Territory> { territory2, territory3 }
            };

            // Persist entities
            context.Employees.Add(employee1);
            context.Employees.Add(employee2);
            context.SaveChanges();

            // Detach entities
            var objContext = ((IObjectContextAdapter)context).ObjectContext;
            objContext.Detach(employee1);
            objContext.Detach(employee2);

            // Clear reference properties
            territory1.Area = null;
            territory2.Area = null;
            territory3.Area = null;
            employee1.Territories = new List<Territory> { territory1, territory2 };
            employee2.Territories = new List<Territory> { territory2, territory3 };

            // Return employees
            return new List<Employee> { employee1, employee2 };
        }