Proper way to delete Parent and Child List

Proper way to delete Parent and Child List

Old forum URL: forums.lhotka.net/forums/t/9567.aspx


ballistic posted on Wednesday, September 22, 2010

I have an Album (BusinessBase) that has a Photos property (BusinessListBase).  These corrolate directly to an album table and a photo table. Where the photo table has an albumId field that is a FK to the id in the album table.

I am unclear as to the proper way to delete the album and the associated photos.

In code, I am looping through the list of photos and then for each photo, calling:
album.PhotoList.Remove(photo);

Once the loop has finished, I then mark the album as deleted:
album.Delete();

And finally commit my changes:
album = album.Save();

When I do this, it first attempts to delete the Album record, which causes integrity problems since the photos still reference the album.

Any suggestions?

RockfordLhotka replied on Thursday, September 23, 2010

Typically your DataPortal_DeleteSelf would do one of the following:

  1. Delele the album header row in the database, allowing the database to do a cascading delete
  2. Call a stored procedure to delete an album, and the stored procedure would delete related data
  3. Update all child objects, then delete the album header row

 

ballistic replied on Thursday, September 23, 2010

I took the third approach.  From within 

DataPortal_DeleteSelf()
I am calling UpdateChildren before I called the album's delete method in the DAL.

FieldManager.UpdateChildren(this);

Copyright (c) Marimer LLC