partial void OnAddNewCore(ref Order item, ref bool cancel);
        //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);
            }
        }
        //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);
        }
        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)
            {
                const string commandText = "UPDATE [dbo].[Orders] SET [UserId] = @p_UserId, [OrderDate] = @p_OrderDate, [ShipAddr1] = @p_ShipAddr1, [ShipAddr2] = @p_ShipAddr2, [ShipCity] = @p_ShipCity, [ShipState] = @p_ShipState, [ShipZip] = @p_ShipZip, [ShipCountry] = @p_ShipCountry, [BillAddr1] = @p_BillAddr1, [BillAddr2] = @p_BillAddr2, [BillCity] = @p_BillCity, [BillState] = @p_BillState, [BillZip] = @p_BillZip, [BillCountry] = @p_BillCountry, [Courier] = @p_Courier, [TotalPrice] = @p_TotalPrice, [BillToFirstName] = @p_BillToFirstName, [BillToLastName] = @p_BillToLastName, [ShipToFirstName] = @p_ShipToFirstName, [ShipToLastName] = @p_ShipToLastName, [AuthorizationNumber] = @p_AuthorizationNumber, [Locale] = @p_Locale WHERE [OrderId] = @p_OrderId";
                using (var connection = new SqlConnection(ADOHelper.ConnectionString))
                {
                    connection.Open();
                    using(var command = new SqlCommand(commandText, connection))
                    {
                        command.Parameters.AddWithValue("@p_OrderId", item.OrderId);
                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);

                        using(var reader = new SafeDataReader(command.ExecuteReader()))
                        {
                            //RecordsAffected: The number of rows changed, inserted, or deleted. -1 for select statements; 0 if no rows were affected, or the statement failed. 
                            if(reader.RecordsAffected == 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.");
    
                            if(reader.Read())
                            {
                                item.OrderId = reader.GetInt32("OrderId");
                            }
                        }
                    }
                }
            }


            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;

            const string commandText = "INSERT INTO [dbo].[Orders] ([UserId], [OrderDate], [ShipAddr1], [ShipAddr2], [ShipCity], [ShipState], [ShipZip], [ShipCountry], [BillAddr1], [BillAddr2], [BillCity], [BillState], [BillZip], [BillCountry], [Courier], [TotalPrice], [BillToFirstName], [BillToLastName], [ShipToFirstName], [ShipToLastName], [AuthorizationNumber], [Locale]) VALUES (@p_UserId, @p_OrderDate, @p_ShipAddr1, @p_ShipAddr2, @p_ShipCity, @p_ShipState, @p_ShipZip, @p_ShipCountry, @p_BillAddr1, @p_BillAddr2, @p_BillCity, @p_BillState, @p_BillZip, @p_BillCountry, @p_Courier, @p_TotalPrice, @p_BillToFirstName, @p_BillToLastName, @p_ShipToFirstName, @p_ShipToLastName, @p_AuthorizationNumber, @p_Locale); SELECT [OrderId] FROM [dbo].[Orders] WHERE OrderId = SCOPE_IDENTITY()";
            using (var connection = new SqlConnection(ADOHelper.ConnectionString))
            {
                connection.Open();
                using(var command = new SqlCommand(commandText, connection))
                {
                    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);

                    using(var reader = new SafeDataReader(command.ExecuteReader()))
                    {
                        if(reader.Read())
                        {
                            item.OrderId = reader.GetInt32("OrderId");
                        }
                    }
                }
            }


            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();
        }