Beispiel #1
0
        /// <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;
            }
        }
Beispiel #2
0
        /// <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();
            }
        }