Cascade delete with ObjectListView

Cascade delete with ObjectListView

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


SonOfPirate posted on Monday, August 13, 2007

I have a Windows Forms application that consists of a root object, let's say, "Catalog" that contains 3 child collections: Artists, Titles and Genres.  Each Artist contains a collection of Titles that belong to that artist.  Likewise, each Genre has a collection of Titles that belong to that genre.

The ArtistTitles and GenreTitles collections are implemented as ObjectListViews on the Catalog.Titles collection.  This ensure that all references to a title, whether obtained directly through the Titles property or indirectly through the Artist or Genre, are pointing to the same instance in memory.  This is working great (thanks to those responsible for the ObjectListView!).  However...

We are displaying each list in an editable grid.  As part of this, we allow the user to remove items from each list.  This uses the IBindingList.Remove(object) on the collection.  So, when viewing the list of artists in the current catalog, we can remove an artist by clicking the delete key which calls IBindingList.Remove on the Artists collection.  Internally, we mark the Artist as deleted and move it to the collection's DeletedItems collection.  Great.

The problem we have found is that when we delete an Artist, we also need to delete all of the titles belonging to that artist.  If we don't we receive a referential integrity error from the database because the foreign key constraint on the Title's Artist column isn't valid (cuz the Artist wasn't saved!).

How best to cascade the delete so that we don't try to save the child items?

I have some thoughts but don't want to lead the witness and would like to hear your ideas/suggestions.

 

Copyright (c) Marimer LLC