New Posts New Posts RSS Feed: Cascading Deletes
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Cascading Deletes

 Post Reply Post Reply
Author
JulianBenjamin View Drop Down
Newbie
Newbie
Avatar

Joined: 03-Aug-2007
Location: United States
Posts: 10
Post Options Post Options   Quote JulianBenjamin Quote  Post ReplyReply Direct Link To This Post Topic: Cascading Deletes
    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


Edited by JulianBenjamin - 01-Sep-2007 at 9:41pm
Back to Top
JulianBenjamin View Drop Down
Newbie
Newbie
Avatar

Joined: 03-Aug-2007
Location: United States
Posts: 10
Post Options Post Options   Quote JulianBenjamin Quote  Post ReplyReply Direct Link To This Post 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?

Back to Top
JulianBenjamin View Drop Down
Newbie
Newbie
Avatar

Joined: 03-Aug-2007
Location: United States
Posts: 10
Post Options Post Options   Quote JulianBenjamin Quote  Post ReplyReply Direct Link To This Post Posted: 01-Sep-2007 at 9:41pm
Found in another post the ShouldRemoveDeletedEntities option on the EntityList.  So it's fine.
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down