static void AddNewOrders(IEnumerable<object[]> orderInfos)
        {
            using (NorthwindEntities northwindEntities = new NorthwindEntities())
            {
                using (TransactionScope scope = new TransactionScope())
                {
                    foreach (object[] orderInfo in orderInfos)
                    {
                        Order order = new Order()
                        {
                            CustomerID = (orderInfo[0] as string),
                            EmployeeID = (orderInfo[1] as int?),
                            OrderDate = (orderInfo[2] as DateTime?),
                            RequiredDate = (orderInfo[3] as DateTime?),
                            ShippedDate = (orderInfo[4] as DateTime?),
                            ShipVia = (orderInfo[5] as int?),
                            Freight = (orderInfo[6] as decimal?),
                            ShipName = (orderInfo[7] as string),
                            ShipAddress = (orderInfo[8] as string),
                            ShipCity = (orderInfo[9] as string),
                            ShipRegion = (orderInfo[10] as string),
                            ShipPostalCode = (orderInfo[11] as string),
                            ShipCountry = (orderInfo[12] as string),
                        };

                        northwindEntities.Orders.Add(order);
                    }

                    northwindEntities.SaveChanges();
                    scope.Complete();
                }
            }
        }
예제 #2
0
        /// <summary>
        /// Retrieve order details stored in a cookie and update order
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="context"></param>
        /// <param name="cookieName"></param>
        private void UpdateOrderDetailsFromCookie(Order entity, NorthwindContext context, string cookieName)
        {
            var headers = this.Request.Headers.GetCookies(cookieName);
            var value = headers.SingleOrDefault(h => h.Cookies.Any(c => c.Name.Equals(cookieName)))
                               .Cookies.SingleOrDefault(c => c.Name.Equals(cookieName)).Value;
            var detailModels = Newtonsoft.Json.JsonConvert.DeserializeObject<IEnumerable<OrderDetailModel>>(value);

            if (entity.Details == null)
            {
                entity.Details = new List<OrderDetail>();
            }

            foreach (var detailModel in detailModels)
            {
                OrderDetail detail = entity.Details.SingleOrDefault(d => d.ProductId == detailModel.ProductId);
                if (detailModel.Quantity <= 0 )
                {
                    if (detail != null)
                    {
                        entity.Details.Remove(detail);
                    }
                    continue;
                }

                var product = context.Products.Find(detailModel.ProductId);
                if (detail == null)
                {
                    detail = detailModel.TransformTo<OrderDetail>();
                    entity.Details.Add(detail);
                    detail.Product = product;
                }
                else
                {
                    detail.UpdateFrom(detailModel);
                }
            }
        }
        internal static void Main()
        {
            var db = new NorthwindEntities();
            using (db)
            {
                var transaction = db.Database.BeginTransaction();
                    try
                    {
                        var header = new Order() { CustomerID = "ALFKI", OrderDate = new DateTime(2014, 08, 21) };
                        var added = db.Orders.Add(header);

                        var rows = new Order_Detail[3];
                        rows[0] = new Order_Detail() { OrderID = added.OrderID, ProductID = 1, Quantity = 100, UnitPrice = 0.80m };
                        rows[1] = new Order_Detail() { OrderID = added.OrderID, ProductID = 2, Quantity = 100, UnitPrice = 1.50m };
                        rows[2] = new Order_Detail() { OrderID = added.OrderID, ProductID = 3, Quantity = 100, UnitPrice = 2.50m };
                        header.Order_Details.Add(rows[0]);
                        header.Order_Details.Add(rows[1]);
                        header.Order_Details.Add(rows[2]);
                        db.SaveChanges();

                        header = new Order() { CustomerID = "ALFKI", OrderDate = new DateTime(2014, 08, 30) };
                        added = db.Orders.Add(header);
                        rows = new Order_Detail[3];
                        //// header.Order_Details.Add(new Order_Detail()); // this row will give exception and the transaction will be rolled back
                        db.SaveChanges();
                        Console.WriteLine("Transaction completed successfully.");
                        transaction.Commit();
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("Transaction not completed. Changes rolled back.");
                        Console.WriteLine(e.Message);
                        transaction.Rollback();
                    }
            }
        }
예제 #4
0
        public List<Order> GetOrderHist(string iD)
        {
            var Orders = new List<Order>();

            using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["Northwind"].ConnectionString))
            {
                var cmd = new SqlCommand("CustOrderHist", cn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("CustomerID", iD);

                cn.Open();
                using (SqlDataReader dr = cmd.ExecuteReader())
                {
                    while (dr.Read())
                    {
                        var temp = new Order();
                        temp.ProductName = dr.GetString(0);
                        temp.Quantity = dr.GetInt32(1);
                        Orders.Add(temp);
                    }
                }

            }
            return Orders;
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="OrderUpdatedEvent" /> class
 /// </summary>
 /// <param name="order">The <see cref="Order" /> which the event relates to</param>
 public OrderUpdatedEvent(Order order)
     : base(string.Format("Order: '{0}' was updated.", order.Name), "Northwind.Web", WebEventCodes.WebExtendedBase + 100)
 {
 }