public void Accept_Changes_Should_Mark_Employee_With_Territories_Unchanged()
        {
            // Arrange
            var northwind = new MockNorthwind();
            var employee = northwind.Employees[0];
            employee.TrackingState = TrackingState.Modified;
            employee.Territories[0].TrackingState = TrackingState.Modified;
            employee.Territories[1].TrackingState = TrackingState.Deleted;
            var territory = new Territory
            {
                TerritoryId = "75070",
                TerritoryDescription = "North Dallas",
                TrackingState = TrackingState.Added,
                Employees = new List<Employee> { employee }
            };
            employee.Territories.Add(territory);

            // Act
            employee.AcceptChanges();

            // Assert
            Assert.IsTrue(employee.GetTrackingStates().All(s => s == TrackingState.Unchanged));
        }
        public void Apply_Changes_Should_Mark_Order_Modified_With_OrderDetails_Added_Modified_Deleted_Unchanged()
        {
            // Arrange
            var context = TestsHelper.CreateNorthwindDbContext(CreateNorthwindDbOptions);
            var order = new MockNorthwind().Orders[0];
            var detail1 = order.OrderDetails[0];
            detail1.OrderDetailId = 0;
            var detail2 = order.OrderDetails[1];
            var detail3 = order.OrderDetails[2];
            var detail4 = order.OrderDetails[3];
            order.TrackingState = TrackingState.Modified;
            detail1.TrackingState = TrackingState.Added;
            detail2.TrackingState = TrackingState.Modified;
            detail3.TrackingState = TrackingState.Deleted;
            detail4.TrackingState = TrackingState.Unchanged;

            // Act
            context.ApplyChanges(order);

            // Assert
            Assert.AreEqual(EntityState.Modified, context.Entry(order).State);
            Assert.AreEqual(EntityState.Added, context.Entry(detail1).State);
            Assert.AreEqual(EntityState.Modified, context.Entry(detail2).State);
            Assert.AreEqual(EntityState.Deleted, context.Entry(detail3).State);
            Assert.AreEqual(EntityState.Unchanged, context.Entry(detail4).State);
            Assert.AreEqual(EntityState.Unchanged, context.Entry(detail1.Product).State);
        }
        public void Apply_Changes_Should_Mark_Modified_Order_With_Multiple_OrderDetails_Added()
        {
            // Arrange
            var context = TestsHelper.CreateNorthwindDbContext(CreateNorthwindDbOptions);
            var order = new MockNorthwind().Orders[0];
            var detail1 = order.OrderDetails[0];
            var detail2 = order.OrderDetails[1];
            detail1.OrderDetailId = 0;
            detail2.OrderDetailId = 0;
            detail1.TrackingState = TrackingState.Added;
            detail2.TrackingState = TrackingState.Added;
            order.TrackingState = TrackingState.Modified;
            order.ModifiedProperties = new List<string> {"OrderDate"};

            // Act
            context.ApplyChanges(order);

            // Assert
            Assert.AreEqual(EntityState.Modified, context.Entry(order).State);
            Assert.AreEqual(EntityState.Added, context.Entry(detail1).State);
            Assert.AreEqual(EntityState.Added, context.Entry(detail2).State);
        }
		public void Apply_Changes_Should_Mark_Deleted_Customer_As_Deleted_And_Deleted_Setting_As_Deleted()
		{
			// NOTE: CustomerSetting will be set to null because customer is deleted.

			// Arrange
			var context = TestsHelper.CreateNorthwindDbContext(CreateNorthwindDbOptions);
			var nw = new MockNorthwind();
			var customer = nw.Customers[0];
			customer.TrackingState = TrackingState.Deleted;
			var setting = customer.CustomerSetting = new CustomerSetting { CustomerId = customer.CustomerId, Setting = "Setting1", Customer = customer };
			setting.TrackingState = TrackingState.Deleted;

			// Act
			context.ApplyChanges(customer);

			// Assert
			Assert.AreEqual(EntityState.Deleted, context.Entry(customer).State);
			Assert.AreEqual(EntityState.Deleted, context.Entry(setting).State);
			Assert.IsNull(customer.CustomerSetting);
		}
		public void Apply_Changes_Should_Mark_Deleted_Order_As_Deleted_And_Deleted_Customer_As_Unchanged()
		{
			// NOTE: We ignore deletes of related M-1 entities to because it may be related
			// to other entities.

			// Arrange
			var context = TestsHelper.CreateNorthwindDbContext(CreateNorthwindDbOptions);
			var nw = new MockNorthwind();
			var order = nw.Orders[0];
			order.TrackingState = TrackingState.Deleted;
			var customer = order.Customer;
			customer.TrackingState = TrackingState.Deleted;

			// Act
			context.ApplyChanges(order);

			// Assert
			Assert.AreEqual(EntityState.Deleted, context.Entry(order).State);
			Assert.AreEqual(EntityState.Unchanged, context.Entry(customer).State);
			Assert.IsNull(order.Customer);
			Assert.IsNull(order.CustomerId);
		}
		public void Apply_Changes_Should_Mark_Deleted_Employee_As_Deleted_And_Added_Territories_As_Unchanged()
		{
			// NOTE: Because parent is deleted, added children will be marked as unchanged
			// but removed from the M-M relation

			// Arrange
			var context = TestsHelper.CreateNorthwindDbContext(CreateNorthwindDbOptions);
			var nw = new MockNorthwind();
			var employee = nw.Employees[0];
			employee.TrackingState = TrackingState.Deleted;
			var territory1 = employee.Territories[0];
			var territory2 = employee.Territories[1];
			var territory3 = employee.Territories[2];
			var territory4 = nw.Territories[3];
			territory4.TrackingState = TrackingState.Added;
			employee.Territories.Add(territory4);

			// Act
			context.ApplyChanges(employee);

			// Assert
			Assert.AreEqual(EntityState.Deleted, 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.Unchanged, context.Entry(territory4).State);
			Assert.IsTrue(context.RelatedItemHasBeenRemoved(employee, territory1));
			Assert.IsTrue(context.RelatedItemHasBeenRemoved(employee, territory2));
			Assert.IsTrue(context.RelatedItemHasBeenRemoved(employee, territory3));
			Assert.IsTrue(context.RelatedItemHasBeenRemoved(employee, territory4));
			Assert.AreEqual(0, employee.Territories.Count);
		}
		public void Apply_Changes_Should_Mark_Added_Customer_As_Added_And_Unchanged_Setting_As_Added()
		{
			// Arrange
			var context = TestsHelper.CreateNorthwindDbContext(CreateNorthwindDbOptions);
			var nw = new MockNorthwind();
			var customer = nw.Customers[0];
			customer.TrackingState = TrackingState.Added;
			var setting = customer.CustomerSetting = new CustomerSetting
				{ CustomerId = customer.CustomerId, Setting = "Setting1", Customer = customer };

			// Act
			context.ApplyChanges(customer);

			// Assert
			Assert.AreEqual(EntityState.Added, context.Entry(customer).State);
			Assert.AreEqual(EntityState.Added, context.Entry(setting).State);
		}
        public void Accept_Changes_Should_Remove_ModifiedProperties_From_Customer_With_CustomerSetting()
        {
            // Arrange
            var northwind = new MockNorthwind();
            var customer = northwind.Customers[0];
            customer.TrackingState = TrackingState.Modified;
            customer.ModifiedProperties = new List<string> { "CustomerName" };
            customer.CustomerSetting = new CustomerSetting
                { TrackingState = TrackingState.Modified };
            customer.CustomerSetting.ModifiedProperties = new List<string> { "Setting" };

            // Act
            customer.AcceptChanges();

            // Assert
            Assert.IsFalse(customer.GetModifiedProperties().Any(p => p != null));
            Assert.IsFalse(customer.CustomerSetting.GetModifiedProperties().Any(p => p != null));
        }
        public void Accept_Changes_Should_Not_Remove_Deleted_CustomerSetting_From_Customer()
        {
            // NOTE: Reference entities cannot be deleted from a related entity, because
            // of possible referential constraints, but they can be deleted independently.

            // Arrange
            var northwind = new MockNorthwind();
            var customer = northwind.Customers[0];
            customer.TrackingState = TrackingState.Modified;
            customer.CustomerSetting = new CustomerSetting { TrackingState = TrackingState.Deleted };

            // Act
            customer.AcceptChanges();

            // Assert
            Assert.IsNotNull(customer.CustomerSetting);
            Assert.AreEqual(TrackingState.Unchanged, customer.CustomerSetting.TrackingState);
        }
        public void Accept_Changes_Should_Remove_ModifiedProperties_From_Order_With_Customer()
        {
            // Arrange
            var northwind = new MockNorthwind();
            var order = northwind.Orders[0];
            order.TrackingState = TrackingState.Modified;
            order.ModifiedProperties = new List<string> { "OrderDate" };
            order.Customer.TrackingState = TrackingState.Modified;
            order.Customer.ModifiedProperties = new List<string> { "CustomerName" };

            // Act
            order.AcceptChanges();

            // Assert
            Assert.IsFalse(order.GetModifiedProperties().Any(p => p != null));
            Assert.IsFalse(order.Customer.GetModifiedProperties().Any(p => p != null));
        }
        public void Accept_Changes_Should_Mark_Customer_With_Deleted_CustomerSetting_Unchanged()
        {
            // Arrange
            var northwind = new MockNorthwind();
            var customer = northwind.Customers[0];
            customer.TrackingState = TrackingState.Modified;
            customer.CustomerSetting = new CustomerSetting
            { TrackingState = TrackingState.Deleted };

            // Act
            customer.AcceptChanges();

            // Assert
            Assert.AreEqual(TrackingState.Unchanged, customer.TrackingState);
            Assert.AreEqual(TrackingState.Unchanged, customer.CustomerSetting.TrackingState);
        }
        public void Accept_Changes_Should_Mark_Order_With_Deleted_Customer_Unchanged()
        {
            // Arrange
            var northwind = new MockNorthwind();
            var order = northwind.Orders[0];
            order.TrackingState = TrackingState.Modified;
            order.Customer.TrackingState = TrackingState.Deleted;

            // Act
            order.AcceptChanges();

            // Assert
            Assert.AreEqual(TrackingState.Unchanged, order.TrackingState);
            Assert.AreEqual(TrackingState.Unchanged, order.Customer.TrackingState);
        }
        public void Accept_Changes_Should_Remove_Deleted_Territories_From_Employee()
        {
            // Arrange
            var northwind = new MockNorthwind();
            var employee = northwind.Employees[0];
            employee.TrackingState = TrackingState.Modified;
            employee.Territories[0].TrackingState = TrackingState.Modified;
            employee.Territories[1].TrackingState = TrackingState.Deleted;
            var territory = new Territory
            {
                TerritoryId = "75070",
                TerritoryDescription = "North Dallas",
                TrackingState = TrackingState.Added,
                Employees = new List<Employee> { employee }
            };
            employee.Territories.Add(territory);

            // Act
            employee.AcceptChanges();

            // Assert
            Assert.AreEqual(3, employee.Territories.Count);
        }
        public void Accept_Changes_Should_Remove_ModifiedProperties_From_Employee_With_Territories()
        {
            // Arrange
            var northwind = new MockNorthwind();
            var employee = northwind.Employees[0];
            employee.TrackingState = TrackingState.Modified;
            employee.ModifiedProperties = new List<string> { "LastName" };
            employee.Territories[0].ModifiedProperties = new List<string> { "TerritoryDescription" };

            // Act
            employee.AcceptChanges();

            // Assert
            IEnumerable<IEnumerable<string>> modifiedProps = employee.GetModifiedProperties();
            Assert.IsFalse(modifiedProps.Any(p => p != null));
        }
		public void Apply_Changes_Should_Mark_Added_Employee_As_Added_And_Modified_Territories_As_Modified()
		{
			// NOTE: Modified children of an added parent will remain modified,
			// but they will be added to the M-M relation.

			// Arrange
			var context = TestsHelper.CreateNorthwindDbContext(CreateNorthwindDbOptions);
			var nw = new MockNorthwind();
			var employee = nw.Employees[0];
			employee.TrackingState = TrackingState.Added;
			var territory1 = employee.Territories[0];
			var territory2 = employee.Territories[1];
			var territory3 = employee.Territories[2];
			territory3.TrackingState = TrackingState.Modified;

			// Act
			context.ApplyChanges(employee);

			// Assert
			Assert.AreEqual(EntityState.Added, context.Entry(employee).State);
			Assert.AreEqual(EntityState.Unchanged, context.Entry(territory1).State);
			Assert.AreEqual(EntityState.Unchanged, context.Entry(territory2).State);
			Assert.AreEqual(EntityState.Modified, context.Entry(territory3).State);
			Assert.IsTrue(context.RelatedItemHasBeenAdded(employee, territory1));
			Assert.IsTrue(context.RelatedItemHasBeenAdded(employee, territory2));
			Assert.IsTrue(context.RelatedItemHasBeenAdded(employee, territory3));
		}
        public void Accept_Changes_Should_Mark_Multiple_Orders_As_Unchanged()
        {
            // Arrange
            var northwind = new MockNorthwind();
            var order1 = northwind.Orders[0];
            order1.TrackingState = TrackingState.Modified;
            order1.Customer.TrackingState = TrackingState.Modified;
            order1.OrderDetails[1].TrackingState = TrackingState.Modified;
            order1.OrderDetails[2].TrackingState = TrackingState.Added;
            order1.OrderDetails[3].TrackingState = TrackingState.Deleted;

            var order2 = northwind.Orders[2];
            order2.Customer.TrackingState = TrackingState.Modified;
            order2.OrderDetails[0].TrackingState = TrackingState.Modified;
            order2.OrderDetails[1].TrackingState = TrackingState.Added;
            order2.OrderDetails[2].TrackingState = TrackingState.Deleted;

            // Act
            var orders = new List<Order> {order1, order2};
            orders.AcceptChanges();

            // Assert
            Assert.AreEqual(TrackingState.Unchanged, order1.TrackingState);
            Assert.AreEqual(TrackingState.Unchanged, order1.Customer.TrackingState);
            Assert.IsFalse(order1.OrderDetails.Any(d => d.TrackingState != TrackingState.Unchanged));
            Assert.AreEqual(TrackingState.Unchanged, order2.TrackingState);
            Assert.AreEqual(TrackingState.Unchanged, order2.Customer.TrackingState);
            Assert.IsFalse(order2.OrderDetails.Any(d => d.TrackingState != TrackingState.Unchanged));
        }
		public void Apply_Changes_Should_Mark_Added_Employee_As_Added_And_Deleted_Territories_As_Deleted()
		{
			// NOTE: If a deleted child is assocated with an added parent, 
			// we will just ignore the delete and add the item, since this is unsupported.

			// Arrange
			var context = TestsHelper.CreateNorthwindDbContext(CreateNorthwindDbOptions);
			var nw = new MockNorthwind();
			var employee = nw.Employees[0];
			employee.TrackingState = TrackingState.Added;
			var territory1 = employee.Territories[0];
			var territory2 = employee.Territories[1];
			var territory3 = employee.Territories[2];
			territory3.TrackingState = TrackingState.Deleted;

			// Act
			context.ApplyChanges(employee);

			// Assert
			Assert.AreEqual(EntityState.Added, 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.IsTrue(context.RelatedItemHasBeenAdded(employee, territory3));
		}
		public void Apply_Changes_Should_Mark_Unchanged_Employee_As_Unchanged_And_Modified_Territories_As_Modified()
		{
			// 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];
			territory3.TrackingState = TrackingState.Modified;

			// 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.Modified, context.Entry(territory3).State);
		}
		public void Apply_Changes_Should_Mark_Deleted_Employee_As_Deleted_And_Deleted_Territories_As_Unchanged()
		{
			// NOTE: If a deleted child is assocated with a deleted parent, 
			// it should be set to unchanged and removed from the M-M relation.

			// Arrange
			var context = TestsHelper.CreateNorthwindDbContext(CreateNorthwindDbOptions);
			var nw = new MockNorthwind();
			var employee = nw.Employees[0];
			employee.TrackingState = TrackingState.Deleted;
			var territory1 = employee.Territories[0];
			var territory2 = employee.Territories[1];
			var territory3 = employee.Territories[2];
			territory3.TrackingState = TrackingState.Deleted;

			// Act
			context.ApplyChanges(employee);

			// Assert
			Assert.AreEqual(EntityState.Deleted, 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.IsTrue(context.RelatedItemHasBeenRemoved(employee, territory1));
			Assert.IsTrue(context.RelatedItemHasBeenRemoved(employee, territory2));
			Assert.IsTrue(context.RelatedItemHasBeenRemoved(employee, territory3));
			Assert.AreEqual(0, employee.Territories.Count);
		}
        public void Apply_Changes_Should_Mark_Unchanged_Employee_As_Unchanged_And_Added_Territories_Without_Employee_As_Unchanged()
        {
            // Arrange
            var context = TestsHelper.CreateNorthwindDbContext(CreateNorthwindDbOptions);
            var nw = new MockNorthwind();
            var employee = nw.Employees[0];
            var territory1 = employee.Territories[0];
            territory1.TrackingState = TrackingState.Added;

            // Causes System.InvalidOperationException:
            // The ObjectStateManager does not contain an ObjectStateEntry with a reference to an object of type 'TrackableEntities.EF.Tests.NorthwindModels.Employee'.
            territory1.Employees = new List<Employee>();

            // Act
            context.ApplyChanges(employee);

            // Assert
            Assert.AreEqual(EntityState.Unchanged, context.Entry(employee).State);
            Assert.AreEqual(EntityState.Unchanged, context.Entry(territory1).State);
            Assert.IsTrue(context.RelatedItemHasBeenAdded(employee, territory1));
        }
        public void Apply_Changes_Should_Mark_Added_Customer_As_Added_And_Unchanged_Setting_Order_OrderDetail_As_Added()
        {
            // NOTE: Customer is added, Order and OrderDetail are added due to 1-M relation

            // Arrange
            var context = TestsHelper.CreateNorthwindDbContext(CreateNorthwindDbOptions);
            var nw = new MockNorthwind();

            var customer = nw.Customers[0];
            customer.TrackingState = TrackingState.Added;

            var customerSetting = new CustomerSetting() { CustomerId = customer.CustomerId, Setting = "Setting1" };
            customer.CustomerSetting = customerSetting;

            var order = new Order() { OrderDate = DateTime.Now };
            customer.Orders = new List<Order>() { order };

            var orderDetail = new OrderDetail() { ProductId = nw.Products[0].ProductId, Quantity = 1, UnitPrice = 1 };
            order.OrderDetails = new List<OrderDetail>() { orderDetail };

            // Act
            context.ApplyChanges(customer);

            // Assert
            Assert.AreEqual(EntityState.Added, context.Entry(customer).State);
            Assert.AreEqual(EntityState.Added, context.Entry(customerSetting).State);
            Assert.AreEqual(EntityState.Added, context.Entry(order).State);
            Assert.AreEqual(EntityState.Added, context.Entry(orderDetail).State);
        }
        public void Apply_Changes_Should_Mark_Unchanged_Employee_As_Unchanged_And_Deleted_Territories_Without_Employee_As_Unchanged()
        {
            // Arrange
            var context = TestsHelper.CreateNorthwindDbContext(CreateNorthwindDbOptions);
            var nw = new MockNorthwind();
            var employee = nw.Employees[0];
            var territory1 = employee.Territories[0];
            territory1.TrackingState = TrackingState.Deleted;

            // Remove employees from territories
            territory1.Employees = new List<Employee>();

            // Act
            context.ApplyChanges(employee);

            // Assert
            Assert.AreEqual(EntityState.Unchanged, context.Entry(employee).State);
            Assert.AreEqual(EntityState.Unchanged, context.Entry(territory1).State);
            Assert.IsTrue(context.RelatedItemHasBeenRemoved(employee, territory1));
        }
		public void Apply_Changes_Should_Mark_Deleted_Order_As_Deleted_And_Added_Customer_As_Added()
		{
			// Arrange
			var context = TestsHelper.CreateNorthwindDbContext(CreateNorthwindDbOptions);
			var nw = new MockNorthwind();
			var order = nw.Orders[0];
			order.TrackingState = TrackingState.Deleted;
			var customer = order.Customer;
			customer.TrackingState = TrackingState.Added;

			// Act
			context.ApplyChanges(order);

			// Assert
			Assert.AreEqual(EntityState.Deleted, context.Entry(order).State);
			Assert.AreEqual(EntityState.Added, context.Entry(customer).State);
		}
        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);
        }
		public void Apply_Changes_Should_Mark_Order_Deleted()
		{
			// Arrange
			var context = TestsHelper.CreateNorthwindDbContext(CreateNorthwindDbOptions);
			var order = new MockNorthwind().Orders[0];
			order.CustomerId = null;
			order.Customer = null;
			var detail1 = order.OrderDetails[0];
			var detail2 = order.OrderDetails[1];
			var detail3 = order.OrderDetails[2];
			var detail4 = order.OrderDetails[3];
			order.TrackingState = TrackingState.Deleted;

			// Act
			context.ApplyChanges(order);

			// Assert
			Assert.AreEqual(EntityState.Deleted, context.Entry(order).State);
			Assert.AreEqual(EntityState.Deleted, context.Entry(detail1).State);
			Assert.AreEqual(EntityState.Deleted, context.Entry(detail2).State);
			Assert.AreEqual(EntityState.Deleted, context.Entry(detail3).State);
			Assert.AreEqual(EntityState.Deleted, context.Entry(detail4).State);
		}
		public void Apply_Changes_Should_Mark_Unchanged_Employee_As_Unchanged_And_Deleted_Territories_As_Unchanged()
		{
			// NOTE: With M-M properties there is no way to tell if the related entity should be deleted
			// or simply removed from the relationship, because it is an independent association.
			// Therefore, deleted children are removed from the relationship and marked unchanged.

			// 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];
			territory3.TrackingState = TrackingState.Deleted;

			// 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.IsTrue(context.RelatedItemHasBeenRemoved(employee, territory3));
			Assert.AreEqual(2, employee.Territories.Count);
		}
        public void Apply_Changes_Should_Mark_Unchanged_Product_Of_Added_OrderDetail_Of_Added_Order_As_Unchanged()
        {
            // Arrange
            var context = TestsHelper.CreateNorthwindDbContext(CreateNorthwindDbOptions);
            var order = new MockNorthwind().Orders[0];
            var orderDetail = order.OrderDetails[0];
            var product = orderDetail.Product;
            order.TrackingState = TrackingState.Added;
            orderDetail.TrackingState = TrackingState.Added;

            // Act
            context.ApplyChanges(order);

            // Assert
            Assert.AreEqual(EntityState.Added, context.Entry(order).State);
            Assert.AreEqual(EntityState.Added, context.Entry(orderDetail).State);
            Assert.AreEqual(EntityState.Unchanged, context.Entry(product).State);
        }
		public void Apply_Changes_Should_Mark_Added_Employee_As_Added_And_Unchanged_Territories_As_Unchanged()
		{
			// NOTE: Because parent is added, unchanged children will be added to M-M relation,
			// even though the entities themselves are unchanged.

			// Arrange
			var context = TestsHelper.CreateNorthwindDbContext(CreateNorthwindDbOptions);
			var nw = new MockNorthwind();
			var employee = nw.Employees[0];
			employee.TrackingState = TrackingState.Added;
			var territory1 = employee.Territories[0];
			var territory2 = employee.Territories[1];
			var territory3 = employee.Territories[2];

			// Act
			context.ApplyChanges(employee);

			// Assert
			Assert.AreEqual(EntityState.Added, 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.IsTrue(context.RelatedItemHasBeenAdded(employee, territory1));
			Assert.IsTrue(context.RelatedItemHasBeenAdded(employee, territory2));
			Assert.IsTrue(context.RelatedItemHasBeenAdded(employee, territory3));
		}
        public void Apply_Changes_Should_Mark_Modified_Order_Unchanged_Customer_With_Addresses_Multiple_Added_And_Unchanged()
        {
            // Arrange
            var context = TestsHelper.CreateNorthwindDbContext(CreateNorthwindDbOptions);
            var order = new MockNorthwind().Orders[0];
            order.OrderDetails = null;
            var address1 = new CustomerAddress
            {
                CustomerAddressId = 0,
                Street = "Street1",
                CustomerId = order.Customer.CustomerId,
                Customer = order.Customer
            };
            var address2 = new CustomerAddress
            {
                CustomerAddressId = 0,
                Street = "Street2",
                CustomerId = order.Customer.CustomerId,
                Customer = order.Customer
            };
            var address3 = new CustomerAddress
            {
                CustomerAddressId = 1,
                Street = "Street3",
                CustomerId = order.Customer.CustomerId,
                Customer = order.Customer
            };
            order.Customer.CustomerAddresses = new List<CustomerAddress> { address1, address2, address3 };
            order.TrackingState = TrackingState.Modified;
            address1.TrackingState = TrackingState.Added;
            address2.TrackingState = TrackingState.Added;
            address3.TrackingState = TrackingState.Unchanged;

            // Act
            context.ApplyChanges(order);

            // Assert
            Assert.AreEqual(EntityState.Modified, context.Entry(order).State);
            Assert.AreEqual(EntityState.Unchanged, context.Entry(order.Customer).State);
            Assert.AreEqual(EntityState.Added, context.Entry(address1).State);
            Assert.AreEqual(EntityState.Added, context.Entry(address2).State);
            Assert.AreEqual(EntityState.Unchanged, context.Entry(address3).State);
        }
        public void Accept_Changes_Should_Not_Remove_Deleted_Customer_From_Order()
        {
            // NOTE: Reference entities cannot be deleted from a related entity, because
            // of possible referential constraints, but they can be deleted independently.

            // Arrange
            var northwind = new MockNorthwind();
            var order = northwind.Orders[0];
            order.TrackingState = TrackingState.Modified;
            order.Customer.TrackingState = TrackingState.Deleted;

            // Act
            order.AcceptChanges();

            // Assert
            Assert.NotNull(order.Customer);
            Assert.Equal(TrackingState.Unchanged, order.Customer.TrackingState);
        }