Reputation: 1307
I currently have an entity model with a bunch of deleted items, the state is deleted. Is there a way to "undelete" them? I know which Items I want to undelete, but I don't know how to undelete the items. Ideally I'd like to get it back to an unchanged state.
Upvotes: 4
Views: 11036
Reputation: 1
you can implement RejectChanges method for your Context, ObjectSet or EntityObject. Now I write VB Code of these methods: Extension method for RejectChanges in Context:
<Extension()>
Sub RejectChanges(ByVal Context As ObjectContext)
Dim Collectin As IEnumerable(Of Object) = From e In Context.ObjectStateManager.GetObjectStateEntries(System.Data.EntityState.Modified Or System.Data.EntityState.Deleted) Select e.Entity
Context.Refresh(RefreshMode.StoreWins, Collectin)
Dim AddedCollection As IEnumerable(Of Object) = From e In Context.ObjectStateManager.GetObjectStateEntries(System.Data.EntityState.Added) Select e.Entity
For Each addedEntity As Object In AddedCollection
Context.Detach(addedEntity)
Next
End Sub
Extension mehod for implementing RejectChanges in ObjectSet:
<Extension()>
Sub RejectChanges(Of T As MyEntity)(ByVal Lst As ObjectSet(Of T))
Dim collection As IEnumerable(Of T) = From o In Lst.AsEnumerable() Where o.EntityState = EntityState.Modified Or o.EntityState = EntityState.Deleted Select o
Lst.Context.Refresh(RefreshMode.StoreWins, collection)
Dim AddedCollection As IEnumerable(Of T) = (From e In Lst.Context.ObjectStateManager.GetObjectStateEntries(System.Data.EntityState.Added) Select e.Entity).ToList().OfType(Of T)()
For Each entity As T In AddedCollection
Lst.Context.Detach(entity)
Next
End Sub
and finally implementing RejectChanges for EntityObject:
<Extension()>
Sub RejectChanges(ByVal entity As EntityObject, ByVal Context As ObjectContext)
If entity.EntityState = EntityState.Modified OrElse entity.EntityState = EntityState.Deleted Then
Context.Refresh(RefreshMode.StoreWins, entity)
ElseIf entity.EntityState = EntityState.Added Then
Context.Detach(entity)
End If
End Sub
bye. [Iman Shabanzade]
Upvotes: 0
Reputation: 1490
Do you have the option of just not committing the connection context? - dispose the ObjectContext without calling objectContext.SaveChanges(); Of course, if you have certain changes that you do wan't saved, they will not persist either.
If you called objectContext.DeleteObject(x)
you can't undelete it and still save changes.
ObjectStateEntry objectStateEntry = objectContext.ObjectStateManager.GetObjectStateEntry(x);
// objectStateEntry.State is not setable
ObjectStateEntry does have the OriginalValues property so you could, in theory, painstakingly recreate a collection that represents the original changes, minus the unwanted ones, exit the objectContext, open a new one and rebuild those changes minus the unwanted ones there. Probably not worth the hassle, but there is no documented way to unmark something for deletion at this time.
Upvotes: 2
Reputation: 81
after you call
objectContext.DeleteObject(x),
you can simulate undelete of object x with
objectContext.Detach(x); objectContext.Attach(x)
Upvotes: 8