protected void DoDelete(ref Supplier 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 SupplierCriteria{SuppId = item.SuppId};
            
            DoDelete(criteria);

            MarkNew(item);
        }
        //Where(a => a.AssociationType == AssociationType.OneToMany  || a.AssociationType == AssociationType.ZeroOrOneToMany  || a.AssociationType == AssociationType.ManyToMany)
        private static void Update_Items_Items_FK__Item__Supplier__1273C1CD(ref Supplier item)
        {
            foreach(Item itemToUpdate in item.Items)
            {
                itemToUpdate.Supplier = item.SuppId;

                new ItemFactory().Update(itemToUpdate, true);
            }
        }
        private void DoUpdate(ref Supplier 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.OriginalSuppId != item.SuppId)
                {
                    // Insert new child.
                    var temp = (Supplier)Activator.CreateInstance(typeof(Supplier), true);
                    temp.SuppId = item.SuppId;
                    temp.Name = item.Name;
                    temp.Status = item.Status;
                    temp.Addr1 = item.Addr1;
                    temp.Addr2 = item.Addr2;
                    temp.City = item.City;
                    temp.State = item.State;
                    temp.Zip = item.Zip;
                    temp.Phone = item.Phone;
                    temp = temp.Save();
    
                    // Mark child lists as dirty. This code may need to be updated to one-to-one relationships.
                    foreach(Item itemToUpdate in item.Items)
                    {
                itemToUpdate.Supplier = item.SuppId;
                    }

                    // Update Children
                    Update_Items_Items_FK__Item__Supplier__1273C1CD(ref item);
    
                    // Delete the old.
                    var criteria = new SupplierCriteria {SuppId = item.OriginalSuppId};
                    
                    Delete(criteria);
    
                    // Mark the original as the new one.
                    item.OriginalSuppId = item.SuppId;

                    MarkOld(item);
                    CheckRules(item);
                    OnUpdated();

                    return;
                }

                const string commandText = "UPDATE [dbo].[Supplier] SET [SuppId] = @p_SuppId, [Name] = @p_Name, [Status] = @p_Status, [Addr1] = @p_Addr1, [Addr2] = @p_Addr2, [City] = @p_City, [State] = @p_State, [Zip] = @p_Zip, [Phone] = @p_Phone WHERE [SuppId] = @p_SuppId; SELECT [SuppId] FROM [dbo].[Supplier] WHERE [SuppId] = @p_OriginalSuppId";
                using (var connection = new SqlConnection(ADOHelper.ConnectionString))
                {
                    connection.Open();
                    using(var command = new SqlCommand(commandText, connection))
                    {
                        command.Parameters.AddWithValue("@p_OriginalSuppId", item.OriginalSuppId);
                command.Parameters.AddWithValue("@p_SuppId", item.SuppId);
                command.Parameters.AddWithValue("@p_Name", ADOHelper.NullCheck(item.Name));
                command.Parameters.AddWithValue("@p_Status", item.Status);
                command.Parameters.AddWithValue("@p_Addr1", ADOHelper.NullCheck(item.Addr1));
                command.Parameters.AddWithValue("@p_Addr2", ADOHelper.NullCheck(item.Addr2));
                command.Parameters.AddWithValue("@p_City", ADOHelper.NullCheck(item.City));
                command.Parameters.AddWithValue("@p_State", ADOHelper.NullCheck(item.State));
                command.Parameters.AddWithValue("@p_Zip", ADOHelper.NullCheck(item.Zip));
                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.OriginalSuppId = item.SuppId;

            MarkOld(item);
            CheckRules(item);

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

            OnUpdated();
        }
        public Supplier Update(Supplier 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 Supplier Update(Supplier item)
 {
     return Update(item, false);
 }
        private void DoInsert(ref Supplier 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].[Supplier] ([SuppId], [Name], [Status], [Addr1], [Addr2], [City], [State], [Zip], [Phone]) VALUES (@p_SuppId, @p_Name, @p_Status, @p_Addr1, @p_Addr2, @p_City, @p_State, @p_Zip, @p_Phone)";
            using (var connection = new SqlConnection(ADOHelper.ConnectionString))
            {
                connection.Open();
                using(var command = new SqlCommand(commandText, connection))
                {
                    command.Parameters.AddWithValue("@p_SuppId", item.SuppId);
                command.Parameters.AddWithValue("@p_Name", ADOHelper.NullCheck(item.Name));
                command.Parameters.AddWithValue("@p_Status", item.Status);
                command.Parameters.AddWithValue("@p_Addr1", ADOHelper.NullCheck(item.Addr1));
                command.Parameters.AddWithValue("@p_Addr2", ADOHelper.NullCheck(item.Addr2));
                command.Parameters.AddWithValue("@p_City", ADOHelper.NullCheck(item.City));
                command.Parameters.AddWithValue("@p_State", ADOHelper.NullCheck(item.State));
                command.Parameters.AddWithValue("@p_Zip", ADOHelper.NullCheck(item.Zip));
                command.Parameters.AddWithValue("@p_Phone", ADOHelper.NullCheck(item.Phone));

                    using(var reader = new SafeDataReader(command.ExecuteReader()))
                    {
                        if(reader.Read())
                        {
                        }
                    }
                }
            }

            item.OriginalSuppId = item.SuppId;

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

            OnInserted();
        }
 partial void OnAddNewCore(ref Supplier item, ref bool cancel);