none
How to revert the deleted object in ObjectContext RRS feed

  • Question

  • Hi,

    I am working on a Form based application using C#.NET 3.5 and Entity Framework3.5. I have an objectcontext. This object is having three entities which are related. Like -

    EntityA can have several EntityB. EntityB can have several EntityC.

    I am showing data in a dialog in above stated relationship. Dialog provides an option to delete EntityB. To delete EntityB i am using following code -

    objectcontext.DeleteObject(EntityB). //this method removed EntityB from EntityA and EntityC from EntityB.

    No i need to provide an option to Undo (Cancel) the delete. How can i achieve this? I tried ObjectManager.GetObjectStateEntries() but no success. I want to undo EntityB and its associated EntityC. Can anyone guide me in this? Any help will be highly appreciable.

    TIA.


    ITChamp

    Saturday, May 19, 2012 4:30 AM

Answers

  • Hi ITChamp;

    To your statement, "It is just changing the object state. It is not again attaching the object to the parent object.", When you delete an object from the ObjectContext it just changes the state of the object from its previous state to Deleted. By changing its state to unchanged it will not be deleted and the relationships should still be intact.

    If for some reason this is not happening please post the actual code you are using so I can see what you are doing.

    Also please tell me how you are verifying that this is not happening.

      

    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Sunday, May 20, 2012 8:43 PM

All replies

  • Hi ITChamp;

    The following should work for you.

    // Get the list of deleted entities from the ObjectStateManager As follows
    IEnumerable<ObjectStateEntry> stateEntryDel = ObjectContextInstanse.ObjectStateManager.GetObjectStateEntries( EntityState.Deleted );

    Then locate the entity / entities you wish to undelete and then do the following on each:

    ObjectContextInstanse.ObjectStateManager.ChangeObjectState( stateEntryDel.ElementAt(IndexWithinCollection).Entity, EntityState.Unchanged );

    Now it is back to the unchanged state.

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Sunday, May 20, 2012 4:21 PM
  • Hi Fernando,

    Thanks for the reply.

    I tried the solution that you provided. But it is not doing the asked behaviour. It is just changing the object state. It is not again attaching the object to the parent object.

    Example -

    EntityB is child of EntityA and EntityC is child of EntityB.

    Now if i delete EntityB from object context and do not call SaveChanges() and user wants to Undo the changes then how can i again attach the deleted EntityB to EntityA [also EntityC should also be attached to EntityB].

    Could you please help me in this regard? Please let me know if you need any more info.

    TIA.


    ITChamp

    Sunday, May 20, 2012 8:24 PM
  • Hi ITChamp;

    To your statement, "It is just changing the object state. It is not again attaching the object to the parent object.", When you delete an object from the ObjectContext it just changes the state of the object from its previous state to Deleted. By changing its state to unchanged it will not be deleted and the relationships should still be intact.

    If for some reason this is not happening please post the actual code you are using so I can see what you are doing.

    Also please tell me how you are verifying that this is not happening.

      

    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Sunday, May 20, 2012 8:43 PM