//Where(a => a.AssociationType == AssociationType.OneToMany  || a.AssociationType == AssociationType.ZeroOrOneToMany  || a.AssociationType == AssociationType.ManyToMany)
        private static void Update_Products_Products_FK__Product__Categor__0CBAE877(ref Category item)
        {
            foreach(Product itemToUpdate in item.Products)
            {
                itemToUpdate.CategoryId = item.CategoryId;

                new ProductFactory().Update(itemToUpdate, true);
            }
        }
 partial void OnAddNewCore(ref Category item, ref bool cancel);
        protected void DoDelete(ref Category 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 CategoryCriteria{CategoryId = item.CategoryId};
            
            DoDelete(criteria);

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

                    // Update Children
                    Update_Products_Products_FK__Product__Categor__0CBAE877(ref item);
    
                    // Delete the old.
                    var criteria = new CategoryCriteria {CategoryId = item.OriginalCategoryId};
                    
                    Delete(criteria);
    
                    // Mark the original as the new one.
                    item.OriginalCategoryId = item.CategoryId;

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

                    return;
                }

                const string commandText = "UPDATE [dbo].[Category] SET [CategoryId] = @p_CategoryId, [Name] = @p_Name, [Descn] = @p_Descn WHERE [CategoryId] = @p_CategoryId; SELECT [CategoryId] FROM [dbo].[Category] WHERE [CategoryId] = @p_OriginalCategoryId";
                using (var connection = new SqlConnection(ADOHelper.ConnectionString))
                {
                    connection.Open();
                    using(var command = new SqlCommand(commandText, connection))
                    {
                        command.Parameters.AddWithValue("@p_OriginalCategoryId", item.OriginalCategoryId);
                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));

                        //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.OriginalCategoryId = item.CategoryId;

            MarkOld(item);
            CheckRules(item);

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

            OnUpdated();
        }
        public Category Update(Category 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 Category Update(Category item)
 {
     return Update(item, false);
 }
        private void DoInsert(ref Category 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].[Category] ([CategoryId], [Name], [Descn]) VALUES (@p_CategoryId, @p_Name, @p_Descn)";
            using (var connection = new SqlConnection(ADOHelper.ConnectionString))
            {
                connection.Open();
                using(var command = new SqlCommand(commandText, connection))
                {
                    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));

                    using(var reader = new SafeDataReader(command.ExecuteReader()))
                    {
                        if(reader.Read())
                        {
                        }
                    }
                }
            }

            item.OriginalCategoryId = item.CategoryId;

            MarkOld(item);
            CheckRules(item);
            
            if(!stopProccessingChildren)
            {
            // Update Child Items.
                Update_Products_Products_FK__Product__Categor__0CBAE877(ref item);
            }

            OnInserted();
        }