none
How to undo changes in Entity framework ? RRS feed

  • Question

  • Hello !

    I'm using entity Framework 6.02 , NET 4.0 , Visual Basic 2013.

    I'm using DBContext.I have a form with 3 bound textboxes . And I want to make a button to undo changes made by user ( that are not saved yet to database) but without query to database.

    In my previous project ( using EF 4 , and ObjectContext) , I was using this Sub , but this not working anymore  in DBContext :

    Public SubDiscardObjectChanges(entityobject As IEntityWithKey, dprov As MyEntities)
    Dimi As IntegerDimisIdentityField As Boolean
    Dimkeymember As EntityKeyMember
    Dimentry As ObjectStateEntry= dprov.ObjectStateManager.GetObjectStateEntry(entityobject.EntityKey)
    Fori = 0 Toentry.OriginalValues.FieldCount - 1
        isIdentityField = False
        For Eachkeymember Inentry.EntityKey.EntityKeyValues If(entry.CurrentValues.DataRecordInfo.FieldMetadata(i).FieldType.Name.Equals(keymember.Key))  Then           isIdentityField = True    Next    If(NotisIdentityField) Thenentry.CurrentValues.SetValue(i, entry.OriginalValues(i))
        Next
    entry.AcceptChanges()
    EndSub

    What can I do ?

    Thank you.

       

     


           


           


           

           

               


               

                   


               


               

           

    Wednesday, February 19, 2014 3:30 PM

Answers

  • Hello !

    I have found another way and I want your opinion :

    MyCollection=(From t In context.MyTable Select t).ToList

    Myobject=Mycollection.First

    Myobject.value=5

    ...........

    To undo changes :

    context.Entry(Myobject).CurrentValues.SetValues(context.Entry(MyObject).OriginalValues)
    context.Entry(MyObject).State = EntityState.Unchanged

    Is this correct ?

    Thank you !


                                               




    • Edited by dcode25 Thursday, February 20, 2014 3:14 PM
    • Marked as answer by Fred BaoModerator Friday, February 28, 2014 3:28 AM
    Thursday, February 20, 2014 3:11 PM

All replies

  • Hello,

    We can get the ObjectContext instance from DbContext instance like below:

    Dim db As DataBaseFirstDBEntities = New DataBaseFirstDBEntities()
    
    Dim value As ObjectContext = CType(db, IObjectContextAdapter).ObjectContext
    

    Since you are using EF 6, then import the System.Data.Entity.Core.Objects namespace instead of System.Data.Objects.

    So have a try to convert DbContext instance to ObjectContext instance and pass the ObjectContext instance to SubDiscardObjectChanges() method().

    If I misunderstand, please let me know.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Thursday, February 20, 2014 3:38 AM
    Moderator
  • Hello !

    I have problems with the first parameter in sub ( entityobject as IentityWithkey)

    when I was using Objectcontext , I pass the current object ( that was modified and I want to undo)

    Now I get a runtime error if I do this.

    An unhandled exception of type 'System.InvalidCastException' occurred in Program.exe

    Additional information: Unable to cast object of type 'System.Data.Entity.DynamicProxies.MyArticle_F0984138F2FB69F19231F58635D119073676E8411F1E9FD1B289A8FF2CDBF5ED' to type 'System.Data.Entity.Core.Objects.DataClasses.IEntityWithKey'.

    what can I do ?

    Thank you !

    Thursday, February 20, 2014 12:38 PM
  • Hello !

    I have found another way and I want your opinion :

    MyCollection=(From t In context.MyTable Select t).ToList

    Myobject=Mycollection.First

    Myobject.value=5

    ...........

    To undo changes :

    context.Entry(Myobject).CurrentValues.SetValues(context.Entry(MyObject).OriginalValues)
    context.Entry(MyObject).State = EntityState.Unchanged

    Is this correct ?

    Thank you !


                                               




    • Edited by dcode25 Thursday, February 20, 2014 3:14 PM
    • Marked as answer by Fred BaoModerator Friday, February 28, 2014 3:28 AM
    Thursday, February 20, 2014 3:11 PM
  • >>Is this correct ?

    Yes, you are right.

    context.Entry(Myobject).CurrentValues.SetValues(context.Entry(MyObject).OriginalValues)
    context.Entry(MyObject).State = EntityState.Unchanged
    Actually, both of the two line codes will have the same function:undo changes. Usually I use the second, it is more simple.

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, February 21, 2014 2:52 AM
    Moderator