//Associations.Where(a => a.AssociationType == AssociationType.ManyToOne || a.AssociationType == AssociationType.ManyToZeroOrOne)
        private static void Update_Profile_Profile_FK_Account_Profiles(ref Account item)
        {
                item.Profile.UniqueID = item.UniqueID;

            new ProfileFactory().Update(item.Profile, true);
        }
 partial void OnAddNewCore(ref Account item, ref bool cancel);
        protected void DoDelete(ref Account 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 AccountCriteria{AccountId = item.AccountId};
            
            DoDelete(criteria);

            MarkNew(item);
        }
        private void DoUpdate(ref Account 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_Account_Update]", connection))
                    {
                        command.CommandType = CommandType.StoredProcedure;
                        command.Parameters.AddWithValue("@p_AccountId", item.AccountId);
                command.Parameters["@p_AccountId"].Direction = ParameterDirection.Input;
                command.Parameters.AddWithValue("@p_UniqueID", item.UniqueID);
                command.Parameters.AddWithValue("@p_Email", item.Email);
                command.Parameters.AddWithValue("@p_FirstName", item.FirstName);
                command.Parameters.AddWithValue("@p_LastName", item.LastName);
                command.Parameters.AddWithValue("@p_Address1", item.Address1);
                command.Parameters.AddWithValue("@p_Address2", ADOHelper.NullCheck(item.Address2));
                command.Parameters.AddWithValue("@p_City", item.City);
                command.Parameters.AddWithValue("@p_State", item.State);
                command.Parameters.AddWithValue("@p_Zip", item.Zip);
                command.Parameters.AddWithValue("@p_Country", item.Country);
                command.Parameters.AddWithValue("@p_Phone", ADOHelper.NullCheck(item.Phone));

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


            MarkOld(item);
            CheckRules(item);

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

            OnUpdated();
        }
        public Account Update(Account 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 Account Update(Account item)
 {
     return Update(item, false);
 }
        private void DoInsert(ref Account 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_Account_Insert]", connection))
                {
                    command.CommandType = CommandType.StoredProcedure;
                    command.Parameters.AddWithValue("@p_AccountId", item.AccountId);
                command.Parameters["@p_AccountId"].Direction = ParameterDirection.Output;
                command.Parameters.AddWithValue("@p_UniqueID", item.UniqueID);
                command.Parameters.AddWithValue("@p_Email", item.Email);
                command.Parameters.AddWithValue("@p_FirstName", item.FirstName);
                command.Parameters.AddWithValue("@p_LastName", item.LastName);
                command.Parameters.AddWithValue("@p_Address1", item.Address1);
                command.Parameters.AddWithValue("@p_Address2", ADOHelper.NullCheck(item.Address2));
                command.Parameters.AddWithValue("@p_City", item.City);
                command.Parameters.AddWithValue("@p_State", item.State);
                command.Parameters.AddWithValue("@p_Zip", item.Zip);
                command.Parameters.AddWithValue("@p_Country", item.Country);
                command.Parameters.AddWithValue("@p_Phone", ADOHelper.NullCheck(item.Phone));

                    command.ExecuteNonQuery();

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


            MarkOld(item);
            CheckRules(item);

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

            OnInserted();
        }