public async Task<IHttpActionResult> PutOrder(Order order)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            _dbContext.ApplyChanges(order);

            try
            {
                await _dbContext.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!_dbContext.Orders.Any(o => o.OrderId == order.OrderId))
                {
                    return Conflict();
                }
                throw;
            }

			await _dbContext.LoadRelatedEntitiesAsync(order);
			order.AcceptChanges();
	        return Ok(order);
        }
        public async Task<IHttpActionResult> PostOrder(Order order)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            order.TrackingState = TrackingState.Added;
            _dbContext.ApplyChanges(order);

            await _dbContext.SaveChangesAsync();

            await _dbContext.LoadRelatedEntitiesAsync(order);
            order.AcceptChanges();
            return CreatedAtRoute("DefaultApi", new { id = order.OrderId }, order);
        }
        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);
        }
        private List<Order> CreateTestOrders(NorthwindDbContext context)
        {
            // Create test entities
            var category1 = new Category
            {
                CategoryName = "Test Category 1"
            };
            var category2 = new Category
            {
                CategoryName = "Test Category 2"
            };
            var product1 = new Product
            {
                ProductName = "Test Product 1",
                UnitPrice = 10M,
                Category = category1
            };
            var product2 = new Product
            {
                ProductName = "Test Product 2",
                UnitPrice = 20M,
                Category = category2
            };
            var product3 = new Product
            {
                ProductName = "Test Product 3",
                UnitPrice = 30M,
                Category = category2
            };
            var customer1 = context.Customers
                .Include(c => c.Territory)
                .Include(c => c.CustomerSetting)
                .Single(c => c.CustomerId == TestCustomerId1);
            var customer2 = context.Customers
                .Include(c => c.Territory)
                .Include(c => c.CustomerSetting)
                .Single(c => c.CustomerId == TestCustomerId1);
            var detail1 = new OrderDetail {Product = product1, Quantity = 11, UnitPrice = 11M};
            var detail2 = new OrderDetail {Product = product2, Quantity = 12, UnitPrice = 12M};
            var detail3 = new OrderDetail {Product = product2, Quantity = 13, UnitPrice = 13M};
            var detail4 = new OrderDetail {Product = product3, Quantity = 14, UnitPrice = 14M};
            var order1 = new Order
            {
                OrderDate = DateTime.Today,
                Customer = customer1,
                OrderDetails = new List<OrderDetail>
                {
                    detail1,
                    detail2,
                }
            };
            var order2 = new Order
            {
                OrderDate = DateTime.Today,
                Customer = customer2,
                OrderDetails = new List<OrderDetail>
                {
                    detail3,
                    detail4,
                }
            };

            // Persist entities
            context.Orders.Add(order1);
            context.Orders.Add(order2);
            context.SaveChanges();

            // Detach entities
            var objContext = ((IObjectContextAdapter) context).ObjectContext;
            objContext.Detach(order1);
            objContext.Detach(order2);

            // Clear reference properties
            product1.Category = null;
            product2.Category = null;
            product3.Category = null;
            customer1.Territory = null;
            customer2.Territory = null;
            customer1.CustomerSetting = null;
            customer2.CustomerSetting = null;
            detail1.Product = null;
            detail2.Product = null;
            detail3.Product = null;
            detail4.Product = null;
            order1.OrderDetails = new List<OrderDetail> { detail1, detail2 };
            order2.OrderDetails = new List<OrderDetail> { detail3, detail4 };

            // Return orders
            return new List<Order> {order1, order2};
        }
Esempio n. 5
0
        public static Order CreateTestOrder(string customerId, int[] productIds)
        {
            using (var context = CreateNorthwindDbContext(CreateNorthwindDbOptions))
            {
                var detail1 = new OrderDetail { ProductId = productIds[0], Quantity = 11, UnitPrice = 11M };
                var detail2 = new OrderDetail { ProductId = productIds[1], Quantity = 12, UnitPrice = 12M };
                var detail3 = new OrderDetail { ProductId = productIds[2], Quantity = 13, UnitPrice = 13M };
                var order = new Order
                {
                    OrderDate = DateTime.Today,
                    CustomerId = customerId,
                    OrderDetails = new List<OrderDetail>
                    {
                        detail1,
                        detail2,
                        detail3
                    }
                };

                context.Orders.Add(order);
                context.SaveChanges();
                return order;
            }
        }