//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)
            {
                const string commandText = "UPDATE [dbo].[Account] SET [UniqueID] = @p_UniqueID, [Email] = @p_Email, [FirstName] = @p_FirstName, [LastName] = @p_LastName, [Address1] = @p_Address1, [Address2] = @p_Address2, [City] = @p_City, [State] = @p_State, [Zip] = @p_Zip, [Country] = @p_Country, [Phone] = @p_Phone WHERE [AccountId] = @p_AccountId";
                using (var connection = new SqlConnection(ADOHelper.ConnectionString))
                {
                    connection.Open();
                    using(var command = new SqlCommand(commandText, connection))
                    {
                        command.Parameters.AddWithValue("@p_AccountId", item.AccountId);
                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));

                        using(var reader = new SafeDataReader(command.ExecuteReader()))
                        {
                            //RecordsAffected: The number of rows changed, inserted, or deleted. -1 for select statements; 0 if no rows were affected, or the statement failed. 
                            if(reader.RecordsAffected == 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.");
    
                            if(reader.Read())
                            {
                                item.AccountId = reader.GetInt32("AccountId");
                            }
                        }
                    }
                }
            }


            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;

            const string commandText = "INSERT INTO [dbo].[Account] ([UniqueID], [Email], [FirstName], [LastName], [Address1], [Address2], [City], [State], [Zip], [Country], [Phone]) VALUES (@p_UniqueID, @p_Email, @p_FirstName, @p_LastName, @p_Address1, @p_Address2, @p_City, @p_State, @p_Zip, @p_Country, @p_Phone); SELECT [AccountId] FROM [dbo].[Account] WHERE AccountId = SCOPE_IDENTITY()";
            using (var connection = new SqlConnection(ADOHelper.ConnectionString))
            {
                connection.Open();
                using(var command = new SqlCommand(commandText, connection))
                {
                    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));

                    using(var reader = new SafeDataReader(command.ExecuteReader()))
                    {
                        if(reader.Read())
                        {
                            item.AccountId = reader.GetInt32("AccountId");
                        }
                    }
                }
            }


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

            OnInserted();
        }