protected void DoDelete(ref Inventory 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 InventoryCriteria{ItemId = item.ItemId};
            
            DoDelete(criteria);

            MarkNew(item);
        }
        private void DoUpdate(ref Inventory item, bool stopProccessingChildren)
        {
            bool cancel = false;
            OnUpdating(ref cancel);
            if (cancel) return;

            // Don't update if the item isn't dirty.
            if (item.IsDirty)
            {
                if(item.OriginalItemId != item.ItemId)
                {
                    // Insert new child.
                    var temp = (Inventory)Activator.CreateInstance(typeof(Inventory), true);
                    temp.ItemId = item.ItemId;
                    temp.Qty = item.Qty;
                    temp = temp.Save();
    
                    // Mark child lists as dirty. This code may need to be updated to one-to-one relationships.

                    // Update Children
    
                    // Delete the old.
                    var criteria = new InventoryCriteria {ItemId = item.OriginalItemId};
                    
                    Delete(criteria);
    
                    // Mark the original as the new one.
                    item.OriginalItemId = item.ItemId;

                    MarkOld(item);
                    CheckRules(item);
                    OnUpdated();

                    return;
                }

                using (var connection = new SqlConnection(ADOHelper.ConnectionString))
                {
                    connection.Open();
                    using(var command = new SqlCommand("[dbo].[CSLA_Inventory_Update]", connection))
                    {
                        command.CommandType = CommandType.StoredProcedure;
                        command.Parameters.AddWithValue("@p_OriginalItemId", item.OriginalItemId);
                command.Parameters.AddWithValue("@p_ItemId", item.ItemId);
                command.Parameters.AddWithValue("@p_Qty", item.Qty);

                        //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.OriginalItemId = item.ItemId;

            MarkOld(item);
            CheckRules(item);

            if(!stopProccessingChildren)
            {
            // Update Child Items.
            }

            OnUpdated();
        }
 public Inventory Update(Inventory item)
 {
     return Update(item, false);
 }
        public Inventory Update(Inventory 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;
        }
        private void DoInsert(ref Inventory 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_Inventory_Insert]", connection))
                {
                    command.CommandType = CommandType.StoredProcedure;
                    command.Parameters.AddWithValue("@p_ItemId", item.ItemId);
                command.Parameters.AddWithValue("@p_Qty", item.Qty);

                    command.ExecuteNonQuery();

                }
            }

            item.OriginalItemId = item.ItemId;

            MarkOld(item);
            CheckRules(item);

            if(!stopProccessingChildren)
            {
            // Update Child Items.
            }

            OnInserted();
        }