Print Page | Close Window

RejectChanges to Parent/Children

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2010
Forum Discription: For .NET 4.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=3163
Printed Date: 19-Sep-2025 at 6:57am


Topic: RejectChanges to Parent/Children
Posted By: scottarlp
Subject: RejectChanges to Parent/Children
Date Posted: 19-Dec-2011 at 6:20am
I must be having a brain cell strike. If I have a parent entity such as Order and create a new Order and then attach new OrderDetail entities to the relation, how do I go about rejecting all of them? I had thought that calling RejectChanges() on Order would also take care of the OrderDetail (nothing commited, just added), but what I'm seeing are OrderDetail records dangling in cache waiting to be saved.
 
I've got a generic Entity in my ViewModel for an edit dialog. It's not aware of the specific entity, so I call RejectChanges() on it if the user cancels. I was hoping I could keep this rather generic and possibly (if needed) cycle through any child relations and cancel them as well.
 
Thanks for your help.
Scott



Replies:
Posted By: sbelini
Date Posted: 19-Dec-2011 at 3:08pm
Hi Scott,
 
To reject all entity changes made you should call mgr.RejectChanges.
 
Note that this will roll back all entity changes since the cache was created or the last SaveChanges call was made, which might also roll back entity changes you don't want to.
 
When calling RejectChanges on an entity rather than on the entityManager, the call will not propagate to the entity's children. You could, however, accomplish it by using reflection. In this case, you'd 'travel' thru the entire entity's hierarchy, rejecting changes to all its children. (and possibly grandchildren, etc, if needed)
That will, however, be a laborious because you'd need to navigate thru all the entities to call RejectChanges.

Before going that path I'd suggest using a "sandbox" EM where you could call mgr.RejectChanges() or simply save and then merge the EntityCache of the sandbox mgr with the main entity manager. You will find detailed information about this approach in the http://drc.ideablade.com/xwiki/bin/view/Documentation/multiple-entitymanagers -



Print Page | Close Window