none
How to undo marking an entity as deleted

    Question

  • I am creating an application to track time log entries.

    The user is allowed to delete an entry in an open timelog.  However, an entry may contain comments and custom fields.  I want to protect the user from accidentally deleting the entry when they don't really want to.

    So, when the user clicks the 'X' button (this is the way they delete an entry) I mark the entry as deleted, and the UI shows a 'deleted' entry element on the page.  I also have an 'undo' link on the page.  When the user clicks on 'undo' the entry should no longer be marked for deletion.

    I seem to be having trouble making this work correctly. 

    When I want to delete the entry, the entry is marked as deleted just fine, but I can't restore the entry to a non-deleted state.  How would I do that?
    Context.DeleteObject(_Entry);
    
    var entryStateBefore = Context.GetDescriptorForEntity(_Entry).State;
    
    Context.UpdateObject(_Entry);
    
    var entryStateAfter = Context.GetDescriptorForEntity(_Entry).State;
    // entryStateAfter remains 'Deleted'
    • Edited by Matt Brunell Tuesday, June 30, 2009 2:04 AM Fix paragraphs
    Monday, June 29, 2009 5:50 PM

Answers

  • Hi Matt,

    There is no single method you can use to undo a DeleteObject() operation. However, depending on the state of the entity before deletion you can follow the operations below in the 3rd column to restore the entity to the state before DeleteObject()

    Previous state

    After DeleteObject()

    To Undo DeleteObject()

    Added

    Detached

    ctx.AddObject()

    Unchanged

    Deleted

    ctx.Detach(), ctx.AttachTo()

    Modified

    Deleted

    ctx.Detach(), ctx.AttachTo(), ctx.UpdateObject()

     

    Let me know if you need more information on this.

     

    Thanks,

    Emre

    • Marked as answer by Matt Brunell Tuesday, June 30, 2009 2:03 AM
    Monday, June 29, 2009 9:17 PM
    Answerer

All replies

  • Hi Matt,

    There is no single method you can use to undo a DeleteObject() operation. However, depending on the state of the entity before deletion you can follow the operations below in the 3rd column to restore the entity to the state before DeleteObject()

    Previous state

    After DeleteObject()

    To Undo DeleteObject()

    Added

    Detached

    ctx.AddObject()

    Unchanged

    Deleted

    ctx.Detach(), ctx.AttachTo()

    Modified

    Deleted

    ctx.Detach(), ctx.AttachTo(), ctx.UpdateObject()

     

    Let me know if you need more information on this.

     

    Thanks,

    Emre

    • Marked as answer by Matt Brunell Tuesday, June 30, 2009 2:03 AM
    Monday, June 29, 2009 9:17 PM
    Answerer
  • Yes, that does work.  Thank you.
    Tuesday, June 30, 2009 2:03 AM