//Where(a => a.AssociationType == AssociationType.OneToMany  || a.AssociationType == AssociationType.ZeroOrOneToMany  || a.AssociationType == AssociationType.ManyToMany)
        private static void Update_Items_Items_FK__Item__ProductId__117F9D94(ref Product item)
        {
            foreach(Item itemToUpdate in item.Items)
            {
                itemToUpdate.ProductId = item.ProductId;

                new ItemFactory().Update(itemToUpdate, true);
            }
        }
        protected void DoDelete(ref Product 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 ProductCriteria{ProductId = item.ProductId};
            
            DoDelete(criteria);

            MarkNew(item);
        }
        //Associations.Where(a => a.AssociationType == AssociationType.ManyToOne || a.AssociationType == AssociationType.ManyToZeroOrOne)
        private static void Update_Category_Category_FK__Product__Categor__0CBAE877(ref Product item)
        {
                item.Category.CategoryId = item.CategoryId;

            new CategoryFactory().Update(item.Category, true);
        }
        private void DoUpdate(ref Product 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.OriginalProductId != item.ProductId)
                {
                    // Insert new child.
                    var temp = (Product)Activator.CreateInstance(typeof(Product), true);
                    temp.ProductId = item.ProductId;
                    temp.CategoryId = item.CategoryId;
                    temp.Name = item.Name;
                    temp.Description = item.Description;
                    temp.Image = item.Image;
                    temp = temp.Save();
    
                    // Mark child lists as dirty. This code may need to be updated to one-to-one relationships.
                    foreach(Item itemToUpdate in item.Items)
                    {
                itemToUpdate.ProductId = item.ProductId;
                    }

                    // Update Children
                    Update_Category_Category_FK__Product__Categor__0CBAE877(ref item);
                    Update_Items_Items_FK__Item__ProductId__117F9D94(ref item);
    
                    // Delete the old.
                    var criteria = new ProductCriteria {ProductId = item.OriginalProductId};
                    
                    Delete(criteria);
    
                    // Mark the original as the new one.
                    item.OriginalProductId = item.ProductId;

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

                    return;
                }

                using (var connection = new SqlConnection(ADOHelper.ConnectionString))
                {
                    connection.Open();
                    using(var command = new SqlCommand("[dbo].[CSLA_Product_Update]", connection))
                    {
                        command.CommandType = CommandType.StoredProcedure;
                        command.Parameters.AddWithValue("@p_OriginalProductId", item.OriginalProductId);
                command.Parameters.AddWithValue("@p_ProductId", item.ProductId);
                command.Parameters.AddWithValue("@p_CategoryId", item.CategoryId);
                command.Parameters.AddWithValue("@p_Name", ADOHelper.NullCheck(item.Name));
                command.Parameters.AddWithValue("@p_Descn", ADOHelper.NullCheck(item.Description));
                command.Parameters.AddWithValue("@p_Image", ADOHelper.NullCheck(item.Image));

                        //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.OriginalProductId = item.ProductId;

            MarkOld(item);
            CheckRules(item);

            if(!stopProccessingChildren)
            {
            // Update Child Items.
                Update_Category_Category_FK__Product__Categor__0CBAE877(ref item);
                Update_Items_Items_FK__Item__ProductId__117F9D94(ref item);
            }

            OnUpdated();
        }
        public Product Update(Product 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 Product Update(Product item)
 {
     return Update(item, false);
 }
        private void DoInsert(ref Product 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_Product_Insert]", connection))
                {
                    command.CommandType = CommandType.StoredProcedure;
                    command.Parameters.AddWithValue("@p_ProductId", item.ProductId);
                command.Parameters.AddWithValue("@p_CategoryId", item.CategoryId);
                command.Parameters.AddWithValue("@p_Name", ADOHelper.NullCheck(item.Name));
                command.Parameters.AddWithValue("@p_Descn", ADOHelper.NullCheck(item.Description));
                command.Parameters.AddWithValue("@p_Image", ADOHelper.NullCheck(item.Image));

                    command.ExecuteNonQuery();

                }
            }

            item.OriginalProductId = item.ProductId;

            MarkOld(item);
            CheckRules(item);

            if(!stopProccessingChildren)
            {
            // Update Child Items.
                Update_Category_Category_FK__Product__Categor__0CBAE877(ref item);
                Update_Items_Items_FK__Item__ProductId__117F9D94(ref item);
            }

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