//Where(a => a.AssociationType == AssociationType.OneToMany  || a.AssociationType == AssociationType.ZeroOrOneToMany  || a.AssociationType == AssociationType.ManyToMany)
        private static void Update_OrderStatuses_OrderStatuses_FK__OrderStat__Order__060DEAE8(ref Order item)
        {
            foreach(OrderStatus itemToUpdate in item.OrderStatuses)
            {
                itemToUpdate.OrderId = item.OrderId;

                new OrderStatusFactory().Update(itemToUpdate, true);
            }
        }
        protected void DoDelete(ref Order item)
        {
            // If we're not dirty then don't update the database.
            if (!item.IsDirty) return;

            // If we're new then don't call delete.
            if (item.IsNew) return;
            
            var criteria = new OrderCriteria{OrderId = item.OrderId};
            
            DoDelete(criteria);

            MarkNew(item);
        }
        //Where(a => a.AssociationType == AssociationType.OneToMany  || a.AssociationType == AssociationType.ZeroOrOneToMany  || a.AssociationType == AssociationType.ManyToMany)
        private static void Update_LineItems_LineItems_FK__LineItem__OrderI__03317E3D(ref Order item)
        {
            foreach(LineItem itemToUpdate in item.LineItems)
            {
                itemToUpdate.OrderId = item.OrderId;

                new LineItemFactory().Update(itemToUpdate, true);
            }
        }
        private void DoUpdate(ref Order item, bool stopProccessingChildren)
        {
            bool cancel = false;
            OnUpdating(ref cancel);
            if (cancel) return;

            // Don't update if the item isn't dirty.
            if (item.IsDirty)
            {
                using (var connection = new SqlConnection(ADOHelper.ConnectionString))
                {
                    connection.Open();
                    using(var command = new SqlCommand("[dbo].[CSLA_Order_Update]", connection))
                    {
                        command.CommandType = CommandType.StoredProcedure;
                        command.Parameters.AddWithValue("@p_OrderId", item.OrderId);
                command.Parameters["@p_OrderId"].Direction = ParameterDirection.Input;
                command.Parameters.AddWithValue("@p_UserId", item.UserId);
                command.Parameters.AddWithValue("@p_OrderDate", item.OrderDate);
                command.Parameters.AddWithValue("@p_ShipAddr1", item.ShipAddr1);
                command.Parameters.AddWithValue("@p_ShipAddr2", ADOHelper.NullCheck(item.ShipAddr2));
                command.Parameters.AddWithValue("@p_ShipCity", item.ShipCity);
                command.Parameters.AddWithValue("@p_ShipState", item.ShipState);
                command.Parameters.AddWithValue("@p_ShipZip", item.ShipZip);
                command.Parameters.AddWithValue("@p_ShipCountry", item.ShipCountry);
                command.Parameters.AddWithValue("@p_BillAddr1", item.BillAddr1);
                command.Parameters.AddWithValue("@p_BillAddr2", ADOHelper.NullCheck(item.BillAddr2));
                command.Parameters.AddWithValue("@p_BillCity", item.BillCity);
                command.Parameters.AddWithValue("@p_BillState", item.BillState);
                command.Parameters.AddWithValue("@p_BillZip", item.BillZip);
                command.Parameters.AddWithValue("@p_BillCountry", item.BillCountry);
                command.Parameters.AddWithValue("@p_Courier", item.Courier);
                command.Parameters.AddWithValue("@p_TotalPrice", item.TotalPrice);
                command.Parameters.AddWithValue("@p_BillToFirstName", item.BillToFirstName);
                command.Parameters.AddWithValue("@p_BillToLastName", item.BillToLastName);
                command.Parameters.AddWithValue("@p_ShipToFirstName", item.ShipToFirstName);
                command.Parameters.AddWithValue("@p_ShipToLastName", item.ShipToLastName);
                command.Parameters.AddWithValue("@p_AuthorizationNumber", item.AuthorizationNumber);
                command.Parameters.AddWithValue("@p_Locale", item.Locale);

                        //result: The number of rows changed, inserted, or deleted. -1 for select statements; 0 if no rows were affected, or the statement failed. 
                        int result = command.ExecuteNonQuery();
                        if (result == 0)
                            throw new DBConcurrencyException("The entity is out of date on the client. Please update the entity and try again. This could also be thrown if the sql statement failed to execute.");

                        item.OrderId = (System.Int32)command.Parameters["@p_OrderId"].Value;
                    }
                }
            }


            MarkOld(item);
            CheckRules(item);

            if(!stopProccessingChildren)
            {
            // Update Child Items.
                Update_LineItems_LineItems_FK__LineItem__OrderI__03317E3D(ref item);
                Update_OrderStatuses_OrderStatuses_FK__OrderStat__Order__060DEAE8(ref item);
            }

            OnUpdated();
        }
        public Order Update(Order item, bool stopProccessingChildren)
        {
            if(item.IsDeleted)
            {
                DoDelete(ref item);
                MarkNew(item);
            }
            else if(item.IsNew)
            {
                DoInsert(ref item, stopProccessingChildren);
            }
            else
            {
                DoUpdate(ref item, stopProccessingChildren);
            }

            return item;
        }
 public Order Update(Order item)
 {
     return Update(item, false);
 }
        private void DoInsert(ref Order item, bool stopProccessingChildren)
        {
            // Don't update if the item isn't dirty.
            if (!item.IsDirty) return;

            bool cancel = false;
            OnInserting(ref cancel);
            if (cancel) return;

            using (var connection = new SqlConnection(ADOHelper.ConnectionString))
            {
                connection.Open();
                using(var command = new SqlCommand("[dbo].[CSLA_Order_Insert]", connection))
                {
                    command.CommandType = CommandType.StoredProcedure;
                    command.Parameters.AddWithValue("@p_OrderId", item.OrderId);
                command.Parameters["@p_OrderId"].Direction = ParameterDirection.Output;
                command.Parameters.AddWithValue("@p_UserId", item.UserId);
                command.Parameters.AddWithValue("@p_OrderDate", item.OrderDate);
                command.Parameters.AddWithValue("@p_ShipAddr1", item.ShipAddr1);
                command.Parameters.AddWithValue("@p_ShipAddr2", ADOHelper.NullCheck(item.ShipAddr2));
                command.Parameters.AddWithValue("@p_ShipCity", item.ShipCity);
                command.Parameters.AddWithValue("@p_ShipState", item.ShipState);
                command.Parameters.AddWithValue("@p_ShipZip", item.ShipZip);
                command.Parameters.AddWithValue("@p_ShipCountry", item.ShipCountry);
                command.Parameters.AddWithValue("@p_BillAddr1", item.BillAddr1);
                command.Parameters.AddWithValue("@p_BillAddr2", ADOHelper.NullCheck(item.BillAddr2));
                command.Parameters.AddWithValue("@p_BillCity", item.BillCity);
                command.Parameters.AddWithValue("@p_BillState", item.BillState);
                command.Parameters.AddWithValue("@p_BillZip", item.BillZip);
                command.Parameters.AddWithValue("@p_BillCountry", item.BillCountry);
                command.Parameters.AddWithValue("@p_Courier", item.Courier);
                command.Parameters.AddWithValue("@p_TotalPrice", item.TotalPrice);
                command.Parameters.AddWithValue("@p_BillToFirstName", item.BillToFirstName);
                command.Parameters.AddWithValue("@p_BillToLastName", item.BillToLastName);
                command.Parameters.AddWithValue("@p_ShipToFirstName", item.ShipToFirstName);
                command.Parameters.AddWithValue("@p_ShipToLastName", item.ShipToLastName);
                command.Parameters.AddWithValue("@p_AuthorizationNumber", item.AuthorizationNumber);
                command.Parameters.AddWithValue("@p_Locale", item.Locale);

                    command.ExecuteNonQuery();

                    item.OrderId = (System.Int32)command.Parameters["@p_OrderId"].Value;
                }
            }


            MarkOld(item);
            CheckRules(item);

            if(!stopProccessingChildren)
            {
            // Update Child Items.
                Update_LineItems_LineItems_FK__LineItem__OrderI__03317E3D(ref item);
                Update_OrderStatuses_OrderStatuses_FK__OrderStat__Order__060DEAE8(ref item);
            }

            OnInserted();
        }
 partial void OnAddNewCore(ref Order item, ref bool cancel);