//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);
            }
        }
        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)
            {
                const string commandText = "UPDATE [dbo].[Profiles] SET [Username] = @p_Username, [ApplicationName] = @p_ApplicationName, [IsAnonymous] = @p_IsAnonymous, [LastActivityDate] = @p_LastActivityDate, [LastUpdatedDate] = @p_LastUpdatedDate WHERE [UniqueID] = @p_UniqueID";
                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_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));

                        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.UniqueID = reader.GetInt32("UniqueID");
                            }
                        }
                    }
                }
            }


            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();
        }
        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);
        }
        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;

            const string commandText = "INSERT INTO [dbo].[Profiles] ([Username], [ApplicationName], [IsAnonymous], [LastActivityDate], [LastUpdatedDate]) VALUES (@p_Username, @p_ApplicationName, @p_IsAnonymous, @p_LastActivityDate, @p_LastUpdatedDate); SELECT [UniqueID] FROM [dbo].[Profiles] WHERE UniqueID = SCOPE_IDENTITY()";
            using (var connection = new SqlConnection(ADOHelper.ConnectionString))
            {
                connection.Open();
                using(var command = new SqlCommand(commandText, connection))
                {
                    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));

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


            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();
        }
Example #8
0
 /// <summary>
 /// CodeSmith generated stub method that is called when updating the child <see cref="Account"/> object.
 /// </summary>
 /// <param name="connection"></param>
 /// <param name="cancel">Value returned from the method indicating whether the object insertion should proceed.</param>
 partial void OnChildUpdating(Profile profile, SqlConnection connection, ref bool cancel);
 partial void OnAddNewCore(ref Profile item, ref bool cancel);