How can I undo changes when a SaveChanges() doesn't succeed?
How can I undo changes when a SaveChanges() doesn't succeed ?
contextObject.Toto.AddObject( new Toto());
try
{
contextObject.SaveChanges();
}
catch
{
// Undo changes !
}
In this sample, I'd like to remove the new Toto object in memory. I don't want to re开发者_如何学编程move it manually. I'd like to synchronize my contextObject to my database.
Microsoft is working on it : Unable to refresh some items in the ObjectContext
Saving Changes and Managing Concurrency:
try
{
// Try to save changes, which may cause a conflict.
int num = context.SaveChanges();
Console.WriteLine("No conflicts. " +
num.ToString() + " updates saved.");
}
catch (OptimisticConcurrencyException)
{
// Resolve the concurrency conflict by refreshing the
// object context before re-saving changes.
context.Refresh(RefreshMode.ClientWins, orders);
// Save changes.
context.SaveChanges();
Console.WriteLine("OptimisticConcurrencyException "
+ "handled and changes saved");
}
精彩评论