0

I read this question but unfortunately not found any solution to update ui after reverting changes.

How can I reject all changes in a Linq to SQL's DataContext?

All of my properties are derived from INotifyPropertyChanged.

var changeSet = dataContext.GetChangeSet();
if (changeSet != null)
{
    var updates = changeSet.Updates.ToArray();
    dataContext.Refresh(RefreshMode.OverwriteCurrentValues, changeSet.Updates);

    foreach (var o in updates)
    {
        INotifyPropertyChanged entity = (INotifyPropertyChanged)o;
        // What i have to do to force them trigger PropertyChanged event ???
    }
}
Cinorid
  • 57
  • 5
  • this is totally up to your design. typically you will need to check with every UI object to see if they need update or not. – Bizhan Aug 11 '18 at 11:06
  • @Bijan Thanks for the reply, but I searching for another solution. The root of problem is dataContext.Refresh() Method that doesn't trigger onPropertyChanged event. Have you any suggestion about this? – Cinorid Aug 11 '18 at 15:19
  • I don't understand your question. why should refresh be triggered on the event? does the breakpoint hit refresh? did you save the context prior to refresh? – Bizhan Aug 11 '18 at 15:24
  • @Bijan Let me explain this a little more. I have a WPF application with some user controls. All UI elements are bound to data context and any change to bounded controls automatically reflected to data context's ChangeSet. If I change a TextBox content in UI, onPropertyChanged event will be fired. But if I call DataContext.Refresh(RefreshMode.OverwriteCurrentValues, changeSet.Updates); noting happens. So I am searching for a way to manually do same behavior. – Cinorid Aug 12 '18 at 15:36
  • IMO the design is flawed if you need this. You should work with view models and only modify entity objects after a user's concent. If they cancel an edit action you just discard changed view model objects and no entity object has ever seen a change. – Gert Arnold Aug 12 '18 at 17:12

1 Answers1

1

First I add a Method to all of my Linq2SQL classes:

public partial class Setting
{
    public void SendPropertyChangedFromOutside(string propName)
    {
        SendPropertyChanged(propName);
    }
}

public partial class User
{
    public void SendPropertyChangedFromOutside(string propName)
    {
        SendPropertyChanged(propName);
    }
}

then call UndoDBChanges method:

public static void UndoDBChanges(System.Data.Linq.DataContext dataContext) {
var changeSet = dataContext.GetChangeSet();
if (changeSet != null)
{
    var updates = changeSet.Updates.ToArray();
    dataContext.Refresh(RefreshMode.OverwriteCurrentValues, changeSet.Updates);

    foreach (var o in updates)
    {
        try
        {
            Type t = o.GetType();
            dynamic obj = Convert.ChangeType(o, t);

            foreach (System.Reflection.PropertyInfo prop in t.GetProperties())
            {
                obj.SendPropertyChangedFromOutside(prop.Name);
            }
        }
        catch
        { }
    }
}}
Cinorid
  • 57
  • 5