Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
WPF MVVM - Cancel ie Revert
Message
De
26/09/2019 16:26:07
 
 
À
Tous
Information générale
Forum:
C#
Catégorie:
Entity Framework
Titre:
WPF MVVM - Cancel ie Revert
Divers
Thread ID:
01671193
Message ID:
01671193
Vues:
65
Hi ,

Best way for Revert ie Cancel

This problem I've solved on this way:

ViewModel class
...
public void cancel()
{
int ln = SelectedPerson.Id;
int index = personCollection.IndexOf(_person);
var operson = _business.FindPersonID(ln);

personCollection[index] = operson;
SelectedPerson = operson;
GoToRowDG(index);

}
...

Business class
...
internal Person FindPersonID(int lnid)
{
_dbContext = new PersonDB();
var o =_dbContext.Person.Find(lnid);
return o;
}


is this good way for revert ie cancel ?

I've read and about RejectChanges or BeginEdit/CancelEdit, but I don't know how that can implemented for my example?
************************************************************************************************************

I've solved on yet two other ways:

ViewModel class
...
void cancel()
{
if (personCollection.Count < 1 || personCollection == null)
{
_business.Cancel();
PersonCollection = null;
SelectedPerson = null;
}
else
{
int index = personCollection.IndexOf(_person);
if (index < 0)
index = personCollection.Count - 1;
_business.Cancel();
GoToRowDG(index);
}
}
...

Business class
...
public void Cancel()
{
// 1. OK - first way
//
var changedEntries = _dbContext.ChangeTracker.Entries()
.Where(x => x.State != EntityState.Unchanged).ToList();

foreach (var entry in changedEntries)
{
switch (entry.State)
{
case EntityState.Modified:
//entry.CurrentValues.SetValues(entry.OriginalValues);
entry.State = EntityState.Unchanged;
break;
case EntityState.Added:
entry.State = EntityState.Detached;
break;
case EntityState.Deleted:
entry.State = EntityState.Unchanged;
break;
}
}

//2. OK - second way
//
//var context = ((IObjectContextAdapter)_dbContext).ObjectContext;
//var changedEntries = _dbContext.ChangeTracker.Entries()
// .Where(x => x.State != EntityState.Unchanged).ToList();
//foreach (var change in changedEntries)
//{
// if (change.State == EntityState.Modified)
// {
// context.Refresh(RefreshMode.StoreWins, change.Entity);
// }
// if (change.State == EntityState.Added)
// {
// context.Detach(change.Entity);
// }
//}
}
...
hm...question, what is the best way?
Any idea, any other solution?

************************************************************************************************************
Thanks
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform