views:

53

answers:

3

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 remove it manually. I'd like to synchronize my contextObject to my database.

A: 

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");
}
Sjoerd
This doesn't solve my problem. I haven't any concurrency exception. I just want to undo my changes in memory (the 'AddObject') and so reset the state manager
Patrice Pezillier
A: 

This can help me : ObjectContext Undo, Cancel?

Patrice Pezillier
A: 

Microsoft is working on it : Unable to refresh some items in the ObjectContext

Patrice Pezillier