示例#1
0
 /// <summary>
 /// CodeSmith generated stub method that is called when updating the child <see cref="Product"/> object.
 /// </summary>
 partial void OnChildUpdating(Category category, SqlConnection connection, ref bool cancel);
        //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;
                }

                using (var connection = new SqlConnection(ADOHelper.ConnectionString))
                {
                    connection.Open();
                    using(var command = new SqlCommand("[dbo].[CSLA_Category_Update]", connection))
                    {
                        command.CommandType = CommandType.StoredProcedure;
                        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;

            using (var connection = new SqlConnection(ADOHelper.ConnectionString))
            {
                connection.Open();
                using(var command = new SqlCommand("[dbo].[CSLA_Category_Insert]", connection))
                {
                    command.CommandType = CommandType.StoredProcedure;
                    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));

                    command.ExecuteNonQuery();

                }
            }

            item.OriginalCategoryId = item.CategoryId;

            MarkOld(item);
            CheckRules(item);

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

            OnInserted();
        }