Cascade delete option

Jun 19, 2010 at 2:12 AM

Hi,

First of all, I should thank you for creating excellent tool and I am quite impressed. I was trying to delete a parent object and I am expecting the child objects are delete automaticall. But I am not sure how to implement it in nHydrate.

For example, I have a table State that list all state name & code as a lookup table. I have another table Employee that has StateID which refered to State table.

I tried the following code, it thrown an exception as "The DELETE statement conflicted with the REFERENCE constraint"

     AmsStatelist stateList = AmsStatelist.SelectUsingPK(3); //get state by id 3
            stateList.Delete(); //mark the object to delete
            stateList.Persist(); //saving the changes..

I changed the code as below and still I get an exception as "Deleted row information cannot be accessed through the row."

     DmsStatelist stateList = DmsStatelist.SelectUsingPK(3);
            stateList.Delete();
            stateList.DmsClientsList.Clear();           
            stateList.Persist();

I am not sure what went wrong.

Pls advice

Shan

 

 

Coordinator
Jun 19, 2010 at 4:57 PM

You cannot work on a deleted object. Please call the code in this order. 

stateList.DmsClientsList.Clear();
stateList.Delete();
stateList.ParentCollection.Persist();

Since you are saving more than one object, you need to save the whole subdomain. You can do this by call the Persist method on the subdomain or the parent collection. They both do the same thing.

stateList.ParentCollection.Persist();
stateList.ParentCollection.SubDomain.Persist();

Jun 19, 2010 at 7:41 PM
I tried the given order, I do get the below exception {"The DELETE statement conflicted with the REFERENCE constraint \"<ForeignKey>\". The conflict occurred in database \"nNhydrate\", table \"dbo.Clients\", column 'StateID'.\r\nThe statement has been terminated."}
Jun 21, 2010 at 2:52 PM

I think that SQL Server have to do CASCADE operations. Simply set cascade property for tables reference. You can do it on the tables diagram.

Coordinator
Jun 22, 2010 at 1:38 AM

You might need to send to me your project and database. You must remove the dependent items before you can remove parent items. In other words if you have a Customer/Orders database you will need to clear the orders collection and then delete the associated customer object and persist the entire subdomain. If you want me to look at your project, please send it to chrisd@nhydrate.org