I am new to CSLA and still trying to get my head round it. I need to know how do we delete a child object through its parent ? e.g If I have a Project class (parent) which has some ProjectResources (child) and I need to delete a project. How do I do that through CSLA ? e.g
Project myProject = Project.GetByProjectID(projectID);
ProjectResourceList resources = myProject.ProjectResources;
myProject.Delete();
if (myProject.IsDeleted)
{
while (resources.Any())
{
myProject.ProjectResources.Remove(resources[0].ProjectResourceID);
}
}
myProject.Save();
Remove() doesnt remove them from the database. Obviously I cant delete the parent object because SQL server will complaint about referential integrity. I don't want any stored proc to handle the cascade delete. Any suggestions will be highly appreciated.
Thanks.
The CSLA collection is an in-memory domain object. When you Remove an item from the collection the item is removed from the active collection and is added to a DeletedList (protected inside the collection).
When you ask the data portal to Update your object graph your data access code needs to remove items in the DeletedList and insert/update items in the active collection.
Versions of CSLA since around 2007 do this automatically by invoking the appropriate Child_Insert, Child_Update, and Child_Delete methods in the child objects contained by your collection.
In any case, you can find detailed information about the data portal operations in the 'Using CSLA 4' book.