I have this service which is Singleton and Single threaded and serves bunch of low volume clients. It uses Entity Framework and Data in SQL Server.
If any one of the client's request to Save Data fails, all the subsequent requests are being failed as every time it is trying to save the original failed data object.
Is there is any way to Undo changes to EF data when save fails?
Thanks in Advance
Entity-models / data-contexts / etc are best handled as units of work. If you need to cancel it, simply discard the context and start with a new one. And if you succeed, discard it anyway! Each request should really be using separate data-contexts, otherwise you can get a range of problems:
Note: you can upgrade to EF 4.1 or 4.2 which makes this job easy as a charm:
context.Entry(myEntity).State = EntityState.Unchanged;
Please refer to this for detail.
Create a partial class to your ObjectContext
generated class, and include the following methods in it (VB, sorry - should be easily transcribed to C#):
Public ReadOnly Property DirtyObjects() As IEnumerable(Of ObjectStateEntry)
Get
Return ObjectStateManager.GetObjectStateEntries(
EntityState.Added Or
EntityState.Deleted Or
EntityState.Modified)
End Get
End Property
Public Overloads Sub Refresh()
For Each entry In DirtyObjects
Select Case entry.State
Case EntityState.Modified
Dim original = entry.OriginalValues
For Each prop In entry.GetModifiedProperties()
Dim ordinal = original.GetOrdinal(prop)
entry.CurrentValues.SetValue(ordinal, original(ordinal))
RaisePropertyChanged(entry.Entity, prop)
Next
entry.AcceptChanges()
Case EntityState.Deleted
'I think I would need to call the above again, cuz it might be
'changed values on a Deleted-state entry too.
entry.ChangeState(EntityState.Unchanged)
Case EntityState.Added
entry.ChangeState(EntityState.Detached)
Case Else
'do nothing
Debug.Fail("It's not supposed to stop here.")
End Select
Next
End Sub
Heads up! In the new upcoming version this feature has became hell of a lot easier.
The Answer to the Question is "You Cannot discard Changes to the Context" instead one has to discard ObjectContext as Marc explained.
You can refresh the entity by calling
context.Refresh(RefreshMode.StoreWins, entity)
so I see no need for RejectChanges.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With