protected override void DataPortal_Update()
        {
            bool cancel = false;
            OnUpdating(ref cancel);
            if (cancel) return;

            if(OriginalProductId != ProductId)
            {
                // Insert new child.
                Product item = new Product {ProductId = ProductId, CategoryId = CategoryId, Name = Name, Description = Description, Image = Image};
                
                item = item.Save();

                // Mark editable child lists as dirty. This code may need to be updated to one-to-one relationships.
                foreach(Item itemToUpdate in Items)
                {
                itemToUpdate.ProductId = ProductId;
                }

                // Create a new connection.
                using (var connection = new SqlConnection(ADOHelper.ConnectionString))
                {
                    connection.Open();
                    FieldManager.UpdateChildren(this, connection);
                }

                // Delete the old.
                var criteria = new ProductCriteria {ProductId = OriginalProductId};
                
                DataPortal_Delete(criteria);

                // Mark the original as the new one.
                OriginalProductId = ProductId;
                OnUpdated();

                return;
            }

            using (var connection = new SqlConnection(ADOHelper.ConnectionString))
            {
                connection.Open();
                using(var command = new SqlCommand("[dbo].[CSLA_Product_Update]", connection))
                {
                    command.CommandType = CommandType.StoredProcedure;
                    command.Parameters.AddWithValue("@p_OriginalProductId", this.OriginalProductId);
                command.Parameters.AddWithValue("@p_ProductId", this.ProductId);
                command.Parameters.AddWithValue("@p_CategoryId", this.CategoryId);
                command.Parameters.AddWithValue("@p_Name", ADOHelper.NullCheck(this.Name));
                command.Parameters.AddWithValue("@p_Descn", ADOHelper.NullCheck(this.Description));
                command.Parameters.AddWithValue("@p_Image", ADOHelper.NullCheck(this.Image));
                    //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.");

                    LoadProperty(_originalProductIdProperty, this.ProductId);
                }

                FieldManager.UpdateChildren(this, connection);
            }

            OnUpdated();
        }
Exemple #2
0
        protected override void DataPortal_Update()
        {
            bool cancel = false;

            OnUpdating(ref cancel);
            if (cancel)
            {
                return;
            }

            if (OriginalProductId != ProductId)
            {
                // Insert new child.
                Product item = new Product {
                    ProductId = ProductId, CategoryId = CategoryId, Name = Name, Description = Description, Image = Image
                };

                item = item.Save();

                // Mark editable child lists as dirty. This code may need to be updated to one-to-one relationships.
                foreach (Item itemToUpdate in Items)
                {
                    itemToUpdate.ProductId = ProductId;
                }

                // Create a new connection.
                using (var connection = new SqlConnection(ADOHelper.ConnectionString))
                {
                    connection.Open();
                    FieldManager.UpdateChildren(this, connection);
                }

                // Delete the old.
                var criteria = new ProductCriteria {
                    ProductId = OriginalProductId
                };

                DataPortal_Delete(criteria);

                // Mark the original as the new one.
                OriginalProductId = ProductId;
                OnUpdated();

                return;
            }

            using (var connection = new SqlConnection(ADOHelper.ConnectionString))
            {
                connection.Open();
                using (var command = new SqlCommand("[dbo].[CSLA_Product_Update]", connection))
                {
                    command.CommandType = CommandType.StoredProcedure;
                    command.Parameters.AddWithValue("@p_OriginalProductId", this.OriginalProductId);
                    command.Parameters.AddWithValue("@p_ProductId", this.ProductId);
                    command.Parameters.AddWithValue("@p_CategoryId", this.CategoryId);
                    command.Parameters.AddWithValue("@p_Name", ADOHelper.NullCheck(this.Name));
                    command.Parameters.AddWithValue("@p_Descn", ADOHelper.NullCheck(this.Description));
                    command.Parameters.AddWithValue("@p_Image", ADOHelper.NullCheck(this.Image));
                    //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.");
                    }

                    LoadProperty(_originalProductIdProperty, this.ProductId);
                }

                FieldManager.UpdateChildren(this, connection);
            }

            OnUpdated();
        }