/// <summary>
 /// Deprecated Method for adding a new object to the Orders EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
 /// </summary>
 public void AddToOrders(Order order)
 {
     base.AddObject("Orders", order);
 }
        protected void SaveButton_Click(object sender, EventArgs e)
        {
            var container = new DeliveryManagerModelContainer();

            Order order;
            var orderIDParam = Request.QueryString[OrderConstants.IDParamName];
            if (orderIDParam != null)
            {
                int orderID = Int32.Parse(orderIDParam);
                order = container.Orders.Single(d => d.Id == orderID);
            }
            else
            {
                order = new Order();
                container.Orders.AddObject(order);
            }
            order.Number = NumberTextBox.Text.Trim();
            order.WithoutDelivery = WithoutDeliveryCheckBox.Checked;
            order.Date = Calendar.SelectedDate;
            order.ClientId = Int32.Parse(ClientsList.SelectedValue);

            foreach (GridViewRow row in EntriesGridView.Rows)
            {
                var hdnField = row.Cells[0].Controls.OfType<HiddenField>().Single();
                var depotEntryId = Int32.Parse(hdnField.Value);
                var depotEntry = container.Entries.Single(de => de.Id == depotEntryId) as DepotEntry;
                var orderEntry = order.OrderEntries.SingleOrDefault(oe => oe.DepotEntry.Id == depotEntryId);

                var chkBox = row.Cells[0].Controls.OfType<CheckBox>().Single();
                if (chkBox.Checked)
                {
                    var txtBox = row.Cells[0].Controls.OfType<TextBox>().Single();
                    int count;
                    if (!Int32.TryParse(txtBox.Text, out count) || count < 1)
                        count = 1;

                    if (orderEntry == null)
                    {
                        orderEntry = new OrderEntry
                        {
                            DepotEntry = depotEntry,
                            DeliveryEntityId = depotEntry.DeliveryEntityId,
                            Cost = depotEntry.DeliveryEntity.Cost,
                            Price = depotEntry.DeliveryEntity.Price
                        };
                        order.OrderEntries.Add(orderEntry);
                    }
                    orderEntry.Count = count;
                }
                else
                {
                    if (orderEntry != null)
                    {
                        // return entities back
                        depotEntry.Count += orderEntry.Count;
                        container.Entries.DeleteObject(orderEntry);
                    }
                }
            }

            container.SaveChanges();

            Response.Redirect("~/Orders/OrdersList.aspx", true);
        }
 /// <summary>
 /// Create a new Order object.
 /// </summary>
 /// <param name="id">Initial value of the Id property.</param>
 /// <param name="number">Initial value of the Number property.</param>
 /// <param name="date">Initial value of the Date property.</param>
 /// <param name="clientId">Initial value of the ClientId property.</param>
 public static Order CreateOrder(global::System.Int32 id, global::System.String number, global::System.DateTime date, global::System.Int32 clientId)
 {
     Order order = new Order();
     order.Id = id;
     order.Number = number;
     order.Date = date;
     order.ClientId = clientId;
     return order;
 }