Delete, Referential Constraint

Delete, Referential Constraint

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


LTW posted on Tuesday, April 07, 2009

I have a child object that has the "Delete_Self(parent)" method called by way of the the parent object implementing the "FieldManager.UpdateChildren()" method. In the Delte_Self method I call a Stored Procedure that Deletes the object. However, the Row that is deleted is the parent of another child, which is represented as a Primary Key/Foreign Key relationship in the database. Therefore, a "reference constraint" error is thrown when the attempt is made to delete the row. What I need to do is have the Child objects deleted before the parent is. My question is, does CSLA have a way to deal with this. I do not see anything  in C# 2008 Business Objects book. My idea was to clear() the child collection and than call the save() method on the child collection inside the "Delete_Self" method of the parent, before I call the stored procedure, but it seems to me CSLA would have something built in to deal with this. Any ideas?

sergeyb replied on Tuesday, April 07, 2009

If it is SQL Server DB, you can setup relationship to cascade deletes.  Otherwise, you will need to override DataPortal_Delete in the parent and manually perform delete operations in desired order by calling deletes of children first, then parent.

 

Sergey Barskiy

Principal Consultant

office: 678.405.0687 | mobile: 404.388.1899

cid:_2_0648EA840648E85C001BBCB886257279
Microsoft Worldwide Partner of the Year | Custom Development Solutions, Technical Innovation

 

From: LTW [mailto:cslanet@lhotka.net]
Sent: Tuesday, April 07, 2009 12:52 PM
To: Sergey Barskiy
Subject: [CSLA .NET] Delete, Referential Constraint

 

I have a child object that has the "Delete_Self(parent)" method called by way of the the parent object implementing the "FieldManager.UpdateChildren()" method. In the Delte_Self method I call a Stored Procedure that Deletes the object. However, the Row that is deleted is the parent of another child, which is represented as a Primary Key/Foreign Key relationship in the database. Therefore, a "reference constraint" error is thrown when the attempt is made to delete the row. What I need to do is have the Child objects deleted before the parent is. My question is, does CSLA have a way to deal with this. I do not see anything  in C# 2008 Business Objects book. My idea was to clear() the child collection and than call the save() method on the child collection inside the "Delete_Self" method of the parent, before I call the stored procedure, but it seems to me CSLA would have something built in to deal with this. Any ideas?


JoeFallon1 replied on Wednesday, April 08, 2009

Sergey is 100% correct.

In my Delete methods I always call DeleteChildren first.Thta is a method I added to my templates so I would not forget to call it. If the parent has no child records then it does not do anything. BUt if child records exist then I call a SP to delete the child records based on the parent ID and then I can safely delete the parent.

Or your SP can be smart enough to know it needs to do the same thing and save you the extra call.

Looping over a collection to remove child objects 1 at a time is not a good plan. If there are 100 children you run 100 seperate delete statements instead of a single delete to remove all of them at once. The collection delete code is really only useful when you are adding removing and editing a few rows in a grid or something. If the user wants them all deleted then you should skip over the collection and "just do it".

Joe

Copyright (c) Marimer LLC