Invalid for root objects - use Delete instead Exception

Invalid for root objects - use Delete instead Exception

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


Burim Ratkoceri posted on Thursday, September 25, 2008

Hi,
I have a BusinessListBase derived collection that is holding BusinessBase derived objects.
I have overridden the AddNewCore to allow Add New Row for a DevExpress Data Grid that I am using.
When I click on the blank space for the new row, the row does get created with no problem. If I decide to cancel the adding of the row (by hitting ESC as and example) the collection tries to mark the child object for deferred deletion and this is where I am getting the exception.
If I am creating the child object by using DataPortal.CreateChild<> instead of DataPortal.Create<>  I don't have a problem which makes sence. However I don't understand why the parent property of the child object is correctly set upon Add (I have tested and confirmed that) but still the object is being recognized as a root object when DeleteChild is called on the business base.
Shouldn't the fact that it belongs to a collection make it a child object and not root?

Thanks,
Burim Ratkoceri

ajj3085 replied on Thursday, September 25, 2008

No, being in a collection does not automatically mean the object is a child.  Only calling MarkAsChild makes an instance a child object.  You can have instances in EditableRootListBase, and none of the items in it are child objects.. they are root objects.

CreateChild will call MarkAsChild for you.. Create does not.  That is why one works at the other does not.

Burim Ratkoceri replied on Thursday, September 25, 2008

Thanks ajj3085,
It seems logical, however why does the BusinessBase derived class reports the collection as its parent through the parent property?

Burim

ajj3085 replied on Thursday, September 25, 2008

Being added to the collection sets the parent.

There's two concepts you're confusing here.

On is parent child, which is a containment relationship.  The child is contained in the parent.  This is what BLB sets when an instance is added.

The other is root and child.  In this relationship, child objects aren't loaded or saved independently.  The root is the controller for them, and usually also starts and commits the database transaction as well.   By default, all Csla objects are root objects, unless MarkAsChild is called. 

You can get more detail in one of the earlier chapters of the book.

HTH
Andy

Copyright (c) Marimer LLC