Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如果SaveChanges()不';你不会成功吗?_C#_.net_Entity Framework_Entity_Undo - Fatal编程技术网

C# 如果SaveChanges()不';你不会成功吗?

C# 如果SaveChanges()不';你不会成功吗?,c#,.net,entity-framework,entity,undo,C#,.net,Entity Framework,Entity,Undo,当SaveChanges()失败时,如何撤消更改 contextObject.Toto.AddObject( new Toto()); try { contextObject.SaveChanges(); } catch { // Undo changes ! } 在这个示例中,我想删除内存中的新Toto对象。我不想手动删除它。我想将contextObject同步到数据库。: : Microsoft正在处理它:Microsoft正在处理它:这并不能解决我的问题。我没有任何

当SaveChanges()失败时,如何撤消更改

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

try
{
    contextObject.SaveChanges();
}
catch
{
      // Undo changes !
}
在这个示例中,我想删除内存中的新Toto对象。我不想手动删除它。我想将contextObject同步到数据库。

:

:


Microsoft正在处理它:

Microsoft正在处理它:

这并不能解决我的问题。我没有任何并发异常。我只想撤销对内存的更改(“AddObject”),然后重置状态管理器。这并不能解决我的问题。我没有任何并发异常。我只想撤消对内存的更改(“AddObject”),然后重置状态管理器
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");
}