コード例 #1
0
ファイル: NSTDataAccess.cs プロジェクト: jblank/code-samples
        /// <summary>
        /// Adds a new order and order details
        /// </summary>
        /// <param name="newOrder"></param>
        /// <returns></returns>
        public int AddOrder(Order newOrder)
        {
            using (var context = new HandledNSTEntities())
            {
                context.Orders.Add(newOrder);
                context.SaveChanges();

                return newOrder.Id;
            }
        }
コード例 #2
0
        public void EditReturnsViewTest()
        {
            var order = new Order
                            {
                                Id = 1,
                                CustomerId = 1
                            };

            _mockDataAccess.Setup(x => x.GetOrderById(order.Id)).Returns(order);

            var result = _controller.Edit(order.Id) as ViewResult;
            Assert.IsNotNull(result);
            var model = result.Model as OrderEditModel;
            Assert.IsNotNull(model);
            Assert.AreEqual(order.Id, model.Id);
            Assert.AreEqual(order.CustomerId, model.CustomerId);
        }
コード例 #3
0
ファイル: NSTDataAccess.cs プロジェクト: jblank/code-samples
        /// <summary>
        /// Updates an existing order and order details
        /// </summary>
        /// <param name="order"></param>
        public void UpdateOrder(Order order)
        {
            using (var context = new HandledNSTEntities())
            {
                // Attaching the parent object will automatically
                // attach all the child details.
                context.Orders.Attach(order);

                // Since these entities can't be self-tracking due
                // to MVC, we need to manually set their state.
                DbEntityEntry orderEntry = context.Entry(order);
                orderEntry.State = EntityState.Modified;

                // We also need to set the state for all the children individually as
                // this will not cascade down from the parent.
                foreach (var detailEntry in order.OrderDetails.Select(context.Entry))
                {
                    ((DbEntityEntry) detailEntry).State = EntityState.Modified;
                }

                context.SaveChanges();
            }
        }