Information générale
Catégorie:
Entity Framework
Titre:
WPF MVVM - Cancel ie Revert
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
Voir le fil de ce thread
Voir le fil de ce thread à partir de ce message seulement
Voir tous les messages de ce thread
Voir tous les messages de ce thread à partir de ce message seulement