C# 如果SaveChanges()不';你不会成功吗?
当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正在处理它:这并不能解决我的问题。我没有任何
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");
}