How can I undo all changes made to the Self-Tracking Entity?

I have a client application that loads multiple STEs through WCF.

Using the WPF application, users can select an object from the ListBox and edit it using the pop-up UserControl. Since UserControl is bound directly to the object, when the user makes changes, he, of course, affects the object.

I would like to provide a Cancel function that undoes all changes made to the object.

Any thoughts?

+2
source share
4 answers

You can save the original copy of the object. And edit the cloned version.
If the user cancels the changes, you simply continue to use the original copy.

+3
source

I would say that when using WPF only in the related PropertyChanged event, save the dictionary with the PropertyName key and the PropertyValue value. And after the restoration of state by reflection

+3
source

I use this solution so far Extension Method

using System.Collections.Generic; using System.Reflection; namespace WpfApplication4 { public static class EFExtensions { /// <summary> /// Rejects changes made by user /// </summary> /// <param name="param">Object implementing IObjectWithChangeTracker interface</param> public static void RejectChanges(this IObjectWithChangeTracker param) { OriginalValuesDictionary ovd = param.ChangeTracker.OriginalValues; PropertyInfo[] propertyInfos = (param.GetType()).GetProperties(); foreach (KeyValuePair<string, object> pair in ovd) { foreach (PropertyInfo property in propertyInfos) { if (property.Name == pair.Key && property.CanWrite) { property.SetValue(param, pair.Value, null); } } } } } } 

Main code

 using System.Linq; namespace WpfApplication4 { public partial class MainWindow { public MainWindow() { InitializeComponent(); using (var db = new PlatEntities()) { PacketEPD pd = (from epd in db.PacketEPD select epd).First(); pd.ChangeTracker.ChangeTrackingEnabled = true; pd.EDNo = "1"; pd.RejectChanges(); } } } } 
+2
source

All Articles