1

SaveChanges()が成功しない場合、どうすれば変更を元に戻すことができますか?

contextObject.Toto.AddObject( new Toto());

try
{
    contextObject.SaveChanges();
}
catch
{
      // Undo changes !
}

このサンプルでは、​​メモリ内の新しいTotoオブジェクトを削除したいと思います。手動で削除したくありません。contextObjectをデータベースに同期させたいのですが。

4

2 に答える 2

1

Microsoftはそれに取り組んでいます:ObjectContextの一部のアイテムを更新できません

于 2010-06-24T08:58:07.290 に答える
0

変更の保存と同時実行の管理

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");
}
于 2010-06-23T13:16:21.067 に答える