partial void OnAddNewCore(ref Profile item, ref bool cancel);
        //Where(a => a.AssociationType == AssociationType.OneToMany  || a.AssociationType == AssociationType.ZeroOrOneToMany  || a.AssociationType == AssociationType.ManyToMany)
        private static void Update_Accounts_Accounts_FK_Account_Profiles(ref Profile item)
        {
            foreach(Account itemToUpdate in item.Accounts)
            {
                itemToUpdate.UniqueID = item.UniqueID;

                new AccountFactory().Update(itemToUpdate, true);
            }
        }
        //Where(a => a.AssociationType == AssociationType.OneToMany  || a.AssociationType == AssociationType.ZeroOrOneToMany  || a.AssociationType == AssociationType.ManyToMany)
        private static void Update_Carts_Carts_FK_Cart_Profiles(ref Profile item)
        {
            foreach(Cart itemToUpdate in item.Carts)
            {
                itemToUpdate.UniqueID = item.UniqueID;

                new CartFactory().Update(itemToUpdate, true);
            }
        }
        protected void DoDelete(ref Profile 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 ProfileCriteria{UniqueID = item.UniqueID};
            
            DoDelete(criteria);

            MarkNew(item);
        }
        private void DoUpdate(ref Profile 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_Profile_Update]", connection))
                    {
                        command.CommandType = CommandType.StoredProcedure;
                        command.Parameters.AddWithValue("@p_UniqueID", item.UniqueID);
                command.Parameters["@p_UniqueID"].Direction = ParameterDirection.Input;
                command.Parameters.AddWithValue("@p_Username", item.Username);
                command.Parameters.AddWithValue("@p_ApplicationName", item.ApplicationName);
                command.Parameters.AddWithValue("@p_IsAnonymous", ADOHelper.NullCheck(item.IsAnonymous));
                command.Parameters.AddWithValue("@p_LastActivityDate", ADOHelper.NullCheck(item.LastActivityDate));
                command.Parameters.AddWithValue("@p_LastUpdatedDate", ADOHelper.NullCheck(item.LastUpdatedDate));

                        //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.UniqueID = (System.Int32)command.Parameters["@p_UniqueID"].Value;
                    }
                }
            }


            MarkOld(item);
            CheckRules(item);

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

            OnUpdated();
        }
        public Profile Update(Profile 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 Profile Update(Profile item)
 {
     return Update(item, false);
 }
        private void DoInsert(ref Profile 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_Profile_Insert]", connection))
                {
                    command.CommandType = CommandType.StoredProcedure;
                    command.Parameters.AddWithValue("@p_UniqueID", item.UniqueID);
                command.Parameters["@p_UniqueID"].Direction = ParameterDirection.Output;
                command.Parameters.AddWithValue("@p_Username", item.Username);
                command.Parameters.AddWithValue("@p_ApplicationName", item.ApplicationName);
                command.Parameters.AddWithValue("@p_IsAnonymous", ADOHelper.NullCheck(item.IsAnonymous));
                command.Parameters.AddWithValue("@p_LastActivityDate", ADOHelper.NullCheck(item.LastActivityDate));
                command.Parameters.AddWithValue("@p_LastUpdatedDate", ADOHelper.NullCheck(item.LastUpdatedDate));

                    command.ExecuteNonQuery();

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


            MarkOld(item);
            CheckRules(item);

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

            OnInserted();
        }