locked
SaveChanges and RollBackChanges Client side RRS feed

  • Question

  • Is there are way to rollback the changes that have been applied to the client context?

     

    For example, I have an entity which I make a change on, call UpdateObject and then SaveChanges.  The SaveChanges fails because of whatever reason on the server.  User says OK, not problem I'll edit something else.  When they attempt to post the next change (on the same context) the previous one is still there and gets reposted.

     

    I want to kick the old one out somehow.

     

    And before you say just create a new DataServiceContext, I have a bunch of reference tables that are loaded and I want to use them for reference lists on an entity, whenever I use a new context the tables (more specifically entities) are no good because they are attached to the old one.

     

    Any ideas?

    Wednesday, December 10, 2008 1:16 AM

Answers

  • In your simple case, you can detach the object by calling DataServiceContext.DetachObject, where the parameter is the same object as one you passed in DataServiceContext.UpdateObject method. This will ensure that the object which you modified is no longer tracked by the context and hence when you attempt to post the next change, the previous one won't be submitted.

    But thing to remember here is that the object you just detached will no longer be tracked by Object Context. If you want to use this object in your next operation also, then you will have to manually undo all the changes and attach the object again or requery the object.

    Hope this helps.
    Thanks
    Pratik
    This posting is provided "AS IS" with no warranties, and confers no rights.
    Thursday, December 11, 2008 5:58 PM
    Moderator

All replies

  • In your simple case, you can detach the object by calling DataServiceContext.DetachObject, where the parameter is the same object as one you passed in DataServiceContext.UpdateObject method. This will ensure that the object which you modified is no longer tracked by the context and hence when you attempt to post the next change, the previous one won't be submitted.

    But thing to remember here is that the object you just detached will no longer be tracked by Object Context. If you want to use this object in your next operation also, then you will have to manually undo all the changes and attach the object again or requery the object.

    Hope this helps.
    Thanks
    Pratik
    This posting is provided "AS IS" with no warranties, and confers no rights.
    Thursday, December 11, 2008 5:58 PM
    Moderator
  • That helps a bit, only one extra line of code, that I can live with.
    Friday, December 12, 2008 4:33 AM