예제 #1
0
        public async Task ApplyChanges_Should_Set_Graph_States()
        {
            // Arrange
            ITrackableRepository <Order> ordersRepo = new TrackableRepository <Order>(_fixture.Context);
            var order = await ordersRepo.FindAsync(1);

            ordersRepo.DetachEntities(order);

            order.OrderDetails[0].TrackingState = TrackingState.Unchanged;
            order.OrderDetails[1].TrackingState = TrackingState.Modified;
            order.OrderDetails[2].TrackingState = TrackingState.Deleted;
            var addedDetail = new OrderDetail
            {
                OrderDetailId = 4,
                OrderId       = 1,
                ProductId     = 3,
                Product       = order.OrderDetails[2].Product,
                UnitPrice     = 40,
                Quantity      = 400,
                TrackingState = TrackingState.Added
            };

            order.OrderDetails.Add(addedDetail);

            // Act
            ordersRepo.ApplyChanges(order);

            // Assert
            Assert.Equal(EntityState.Unchanged, _fixture.Context.Entry(order).State);
            Assert.Equal(EntityState.Unchanged, _fixture.Context.Entry(order.OrderDetails[0]).State);
            Assert.Equal(EntityState.Modified, _fixture.Context.Entry(order.OrderDetails[1]).State);
            Assert.Equal(EntityState.Deleted, _fixture.Context.Entry(order.OrderDetails[2]).State);
            Assert.Equal(EntityState.Added, _fixture.Context.Entry(order.OrderDetails[3]).State);
        }
예제 #2
0
        public async Task ApplyChanges_Should_Set_Graph_States()
        {
            // Arrange
            ITrackableRepository <Order> ordersRepo = new TrackableRepository <Order>(_fixture.Context);
            var order = await ordersRepo.FindAsync(10248);

            ordersRepo.DetachEntities(order);

            // updating object in complex object graph
            order.OrderDetails[0].UnitPrice     = 17;
            order.OrderDetails[0].TrackingState = TrackingState.Modified;

            // updating object in complex object graph
            order.OrderDetails[1].Quantity      = 2;
            order.OrderDetails[1].TrackingState = TrackingState.Modified;

            // deleting object in complex object graph
            order.OrderDetails[2].TrackingState = TrackingState.Deleted;

            // adding object in complex object graph
            var addedDetail = new OrderDetail
            {
                //OrderDetailId = 4,
                OrderId       = 1,
                ProductId     = 3,
                Product       = order.OrderDetails[2].Product,
                UnitPrice     = 40,
                Quantity      = 400,
                TrackingState = TrackingState.Added
            };

            // Act
            order.OrderDetails.Add(addedDetail);

            // Act
            ordersRepo.ApplyChanges(order);

            // Assert
            Assert.Equal(EntityState.Unchanged, _fixture.Context.Entry(order).State);
            Assert.Equal(EntityState.Modified, _fixture.Context.Entry(order.OrderDetails[0]).State);
            Assert.Equal(EntityState.Modified, _fixture.Context.Entry(order.OrderDetails[1]).State);
            Assert.Equal(EntityState.Deleted, _fixture.Context.Entry(order.OrderDetails[2]).State);
            Assert.Equal(EntityState.Added, _fixture.Context.Entry(order.OrderDetails[3]).State);

            // Save changes to object graph with different TrackingStates throughout the object graph
            await _unitOfWork.SaveChangesAsync();
        }