Undo changes made to a entity row
Undo changes to a Row that's bound to text box's in a form.
I use the entity frame开发者_StackOverflow中文版work to save and update the data so I have no control on what gets saved. If the user hits Cancel on the form I want all the changes to the row to get reverted back to what they where before.
The same problem I have with new row's I have the new row created on starting the form, how can I get rid of it before it gets saved to the database?
Thanks a lot.
The correct way is to dispose context with modified data, create new context and reload data from database. EF doesn't have any direct way to cancel changes. If you don't want to do it you must manually iterate entities in ObjectStateManager
(ChangeTracker
in DbContext API) and based on their state either detach them (for added rows) or rewrite actual values with original values. If you also modified relations the task will become really complex and in such case it is really good idea to follow my former advice.
You can have a look at Transactions to make such a "fallback". Look at page about TransactionScope which covers the basic in C# syntax. The TransactionScope remember the actions done to database and can rollback them later (ie within same session).
This is not, though, a pure solution by Entity (Generator), you have to make some coding.
精彩评论