I'm using EF Core with WPF and I'd like to revert any changes done on any objects to their initial state when a user press Cancel instead of OK on a dialog, is that possible and how?
I'm using a global singleton DbContext where I load all my data model at the beginning of the application. I do not want to know if I should or should not use a DataContext singleton.
When a user has to do some changes on instances in a Database, I present a WPF DialogBox where he can choose "OK" or "Cancel". On OK I just do ctx.SaveChanges(). But for Cancel, how can I revert back every changes? How to come back to a state where all objects returns to their initial state as when the Dialog was called?
I can Dispose the DataContext (which will flush all changes) and Reload everything again but it takes a lot of time and there should be a better way to achieve the task more efficently by using changes tracked by the DbContext?
I found GitHub-dotnet/efcore request: Implement RejectChanges() in DbContext #14594. but it does'nt seems to have any solution?
I think the proper solution should be close to this answer for EF (not core): DbContext discard changes without disposing. I will try to code it (if possible) but an already properly coded solution, and debugged, would be so great!
Update 2022-05-27
After few trials and errors (like having a singleton Context), I decided to go with something that would require more works but that with be more in line with the EF Core philosophy. In my case, I load the full model (almost) in memory with "NoTracking". Then when I want to edit an instance (entity), I do so by copying it and makes modification on the copy. If the user choose to apply modifications, then I open a Context and attach to the entity to edit, apply changes to the original entity (copy changes from the copy), and then Ctx.SaveChanges and then Dispose().