Print Page | Close Window

Cascading Deletes

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce Classic
Forum Discription: For .NET 2.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=398
Printed Date: 08-Apr-2025 at 5:38pm


Topic: Cascading Deletes
Posted By: JulianBenjamin
Subject: Cascading Deletes
Date Posted: 01-Sep-2007 at 6:03pm
***  This is fixed. ***
 
When deleting related objects (such as addresses, contacts for an agency), what's the best way of doing this?  There's no direct relation (on the database) between the parent and child, as the addresses and contacts could contain records for other entities such as clients, organizations, etc.  They are tied to each other by an EntityId column of type Guid, so it's unique, and DevForce has this mapping as well to load the related records.
What I'm doing in the Delete method is creating an EntityList<Entity>, recursing through the contacts and addresses objects of the item being deleted, adding them to the EntityList and calling the delete function on them.  Then I add the Agency object to the list, call it's Delete function, and call the Save method on the PersistenceManager passing the EntityList to it.  Is this the best (or only) way?  I've included the Delete function below.
 

        public static void Delete(Agency agency)
        {
            EntityList<Entity> changedEntities = new EntityList<Entity>();
            foreach(Contact contact in agency.Contacts)
            {
                changedEntities.Add(contact);
                contact.Delete();
            }
            foreach(Note note in agency.Notes)
            {
                changedEntities.Add(note);
                note.Delete();
            }
            foreach(AgencyClients agencyclient in agency.Clients)
            {
                changedEntities.Add(agencyclient);
                agencyclient.Delete();
            }
            foreach(Address address in agency.Addresses)
            {
                changedEntities.Add(address);
                address.Delete();
            }
            changedEntities.Add(agency);
            agency.Delete();
            mPersManager.SaveChanges(changedEntities);
        }
 
Thanks,
 
Julian



Replies:
Posted By: JulianBenjamin
Date Posted: 01-Sep-2007 at 9:33pm

Actually, I had to change the code, as the enumerated list changed each time I called "delete".

But, how do I get it removed from the database?  After I add it to the entitylist, it gets removed when I call the delete method.  So the call to SaveChanges gets an empty EntityList.  Is there something I'm missing?



Posted By: JulianBenjamin
Date Posted: 01-Sep-2007 at 9:41pm
Found in another post the ShouldRemoveDeletedEntities option on the EntityList.  So it's fine.



Print Page | Close Window