CSLA .NET

Vibrant discussion about CSLA .NET and using the framework to build great business applications.

Deletion - parent/child

rated by 0 users
Answered (Not Verified) This post has 0 verified answers | 6 Replies | 1 Follower

Top 150 Contributor
56 Posts
fredg posted on Tue, Feb 14 2012 3:56 PM

Hi,

    When deleting parent, all child collections should be cleared first.

    Is there sample code to demonstrate this situation in DataPortal_DeleteSelf() of the parent BO?

    I clear the child collection in this method and then delete parent, but the child still exist in database.

All Replies

Top 10 Contributor
1,770 Posts
Answered (Not Verified) JonnyBee replied on Tue, Feb 14 2012 10:43 PM
Suggested by JonnyBee

Hi,

I prefer to use cascading delete in the database.

There could be other relationships too, than the child objects that you have loaded into memory, so this is typically bettter handled in the database (and simpler to code).

Jonny Bekkum, Norway CslaContrib Coordinator

Top 150 Contributor
56 Posts
fredg replied on Wed, Feb 15 2012 1:06 PM

Thanks for your reply. cascading delete in the database is a good option. However, I'm not quite understanding it in CSLA.net. If we recursively clear child collection in parent BO's delete method before deleting parent self, why not child deletion does not automatically call itself's deletion method and remove the records from database? If we want to do it in BO, what code looks like?

My code:

 

void DataPortal_DeleteSelf(Parent Parent)
{

    //clear child collection
    for (int childNumber = ChildCollection.Count() - 1; childNumber >= 0; childNumber--)
    {     
       ChildCollection.RemoveAt(childNumber);
    }

    ....delete parent....

}

 

Do you have correction for my code?

 

 

 

Top 10 Contributor
1,770 Posts

Remove will only move the item to DeleteItems list but does not do any data access.

Jonny Bekkum, Norway CslaContrib Coordinator

Top 150 Contributor
56 Posts
fredg replied on Wed, Feb 15 2012 1:18 PM

I see. Do you have any sample code to recommand to replace my child collection clear part's code to make data access happen?

    //clear child collection
    for (int childNumber = ChildCollection.Count() - 1; childNumber >= 0; childNumber--)
    {     
       ChildCollection.RemoveAt(childNumber);
    }

Top 150 Contributor
56 Posts
fredg replied on Thu, Feb 16 2012 2:45 PM

Hi, can anyone provide some sample of cascading deleting children before deleting parent from BO layer instead of from DA layer?

Top 150 Contributor
56 Posts
fredg replied on Fri, Feb 17 2012 10:49 AM

After adding:

FieldManager.UpdateChildren(this);

Child's Data access code is called, but if the child table has FK constraint, there would be an error. Looks like we have to remove the FK constraint from child table.

Page 1 of 1 (7 items) | RSS

Copyright (c) 2006-2010 Marimer LLC. All rights reserved.
Email admin@lhotka.net for support.
Powered by Community Server (Non-Commercial Edition), by Telligent Systems