0

I am using EF6 DB First and I have a db with one table:

   public partial class Bank:INotifyPropertyChanged
    {
        private int _id;
        private string _name;
        private bool? _deactivate;

        public int Id
        {
            get { return _id; }
            set
            {
                if (value == _id) return;
                _id = value;
                OnPropertyChanged();
            }
        }

        public string Name
        {
            get { return _name; }
            set
            {
                if (value == _name) return;
                _name = value;
                OnPropertyChanged();
            }
        }

        public bool? Deactivate
        {
            get { return _deactivate; }
            set
            {
                if (value.Equals(_deactivate)) return;
                _deactivate = value;
                OnPropertyChanged();
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        [NotifyPropertyChangedInvocator]
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }

Then I created an ObservableCollection from this entity and two button for save and cancel changes:

private readonly MyContext _dbContext;
public ObservableCollection<Bank> Banks { get; set; }

public MainWindow()
{
    InitializeComponent();
    _dbContext = new MyContext();
    _dbContext.Banks.Load();
    Banks = _dbContext.Banks.Local;
    GridCtrl.ItemsSource = Banks;
}

private void SaveButton_OnClick(object sender, RoutedEventArgs e)
{
    _dbContext.SaveChanges();
}

private void CancelButton_OnClick(object sender, RoutedEventArgs e)
{
    foreach (DbEntityEntry entry in _dbContext.ChangeTracker.Entries())
    {
        switch (entry.State)
        {
            case EntityState.Modified:
                entry.State = EntityState.Unchanged;
                break;
            case EntityState.Added:
                entry.State = EntityState.Detached;
                break;
            case EntityState.Deleted:
                entry.Reload();
                break;
        }
    }
}   

Everything is going well except when I delete a row then click cancel button the deleted row does not return back and the observable collection was not affected.

Thanks in advance

ASh
  • 34,632
  • 9
  • 60
  • 82

1 Answers1

0

I have found a solution that I must add entry.State = EntityState.Modified to Revert changes made to deleted entity.

private void CancelButton_OnClick(object sender, RoutedEventArgs e)
{
    foreach (DbEntityEntry entry in _dbContext.ChangeTracker.Entries())
    {
        switch (entry.State)
        {
            case EntityState.Modified:
                entry.State = EntityState.Unchanged;
                break;
            case EntityState.Added:
                entry.State = EntityState.Detached;
                break;
            case EntityState.Deleted:
            entry.State = EntityState.Modified; //Revert changes made to deleted entity.
                entry.Reload() //OR entry.State = EntityState.Unchanged;;
                break;
        }
    }
} 

Source:

DbContext discard changes without disposing

Undo changes in entity framework entities

But I faced another problem :( the returned row take a place to be the last row for ObservableCollection.

example i have 3 rows
- row1
- row2 -> I will delete this row
- row3

when undo changes:
- row1
- row3
- row2 -> will be at the end of the collection
  • You should have closed your own question as duplicate (or deleted it) and asked the bottom part in a new question if it's a problem for you. It's not StackOverflow's style to ask new questions in answers. – Gert Arnold Sep 08 '17 at 14:35