Recursive deletion

Posts   
 
    
orenpeled
User
Posts: 53
Joined: 25-Jul-2005
# Posted on: 29-Mar-2006 09:30:40   

Hi all,

I have table "A" referenced by table "B", while table "B" is referenced by many other tables (1:n, m:n), and I want to delete all records from all tables eventually referencing table "A".

So far I found 2 ways to perform a recursive deletion.

1. Fetching the "A" entity (including the prefetch definition for all referencing tables) and then delete each entity if it is not null and each collection if it is not empty:

IPrefetchPath2 pp = ........ adapter.FetchEntity(...., pp); if(BEntity.C != null) adapter.DeleteEntity(BEntity.C); . . . if(BEntity.Z != null) adapter.DeleteEntity(BEntity.Z); adapter.DeleteEntity(BEntity); adapter.DeleteEntity(AEntity);

Something like that... I don't like this solution, but I would like to get comments for that.

2. Using UnitOfWork2 - but here I have a problem. I figured out that I need to define for each AddDeleteEntitiesDirectlyCall an IRelationPredicateBucket, and it is a lot of work. Isn't there an elegant way to do this?

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 29-Mar-2006 16:14:59   

You can Define Cascade Deletes in the database, so that deleting an Entity will delete records that reference it along the way.

Or you can use a stored procedure.