实体框架5.0处理乐观并发异常? [英] Entity framework 5.0 handle optimistic concurrency exception?

查看:84
本文介绍了实体框架5.0处理乐观并发异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

context.SaveChanges()中处理多个潜在异常时,其中之一是 OptimisticConcurrency 。 Microsoft在http://msdn.microsoft.com/zh-cn/ library / bb399228.aspx 在EF 4.x上对此进行了讨论...

  try 
{
//尝试保存更改,这可能会导致冲突。
int num = context.SaveChanges();
Console.WriteLine(无冲突。 +
num.ToString()+已保存更新。);
}
catch(OptimisticConcurrencyException)
{
//通过在重新保存更改之前刷新
//对象上下文来解决并发冲突。
context.Refresh(RefreshMode.ClientWins,订单);

//保存更改。
context.SaveChanges();
Console.WriteLine( OptimisticConcurrencyException
+已处理并保存更改);
}

...但是在EF 5.0(RC)上,这似乎没有之所以能够正常工作,是因为在我的EF5(代码优先,DbContext派生的 context 类)上不存在 Refresh()。 / p>

我确实看到了 context.Entry(context.SalesOrderHeaders).Reload(); -但这似乎是一个直接从数据库重新加载而不是刷新/合并(在策略客户端获胜的情况下)。



有什么想法如何在EF5中处理乐观并发异常?实际上,即使是SaveChanges()中异常处理的一般指针也很好



谢谢

解决方案

在DbContext API中解决并发异常的方法将重新加载原始实体:

  catch(DbUpdateConcurrencyException ex)
{
//获取失败的条目
var entry = ex.Entries.Single(...);
//用数据库中的值覆盖原始值,但不要
//触摸保存更改的当前值
entry.OriginalValues.SetValues(entry.GetDatabaseValues());
}

您还应该能够使用上述代码,但必须获得<$ DbContext 实例中的c $ c> ObjectContext 实例(它只是 ObjectContext

  catch(DbUpdateConcurrencyException ex)
{
var objContext =((IObjectContextAdapter)context) .ObjectContext;
//获取失败的条目
var entry = ex.Entries.Single(...);
//现在在ObjectContext上调用刷新
objContext.Refresh(RefreshMode.ClientWins,entry.Entity);
}

您甚至可以尝试:

  objContext.Refresh(RefreshMode.ClientWins,ex.Entries.Select(e => e.Entity)); 


When handling several potential exceptions during a context.SaveChanges() one of the exceptions is OptimisticConcurrency. Microsoft's documentation on this at http://msdn.microsoft.com/en-us/library/bb399228.aspx discusses this for EF 4.x ...

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");
}

... but on EF 5.0 (RC), this doesn't seem to work because Refresh() doesn't exist on my EF5, code-first, DbContext derived context class.

I do see context.Entry(context.SalesOrderHeaders).Reload(); - but that appears to be a straightup reload-from-db and not a refresh/merge (with policy client wins).

Any ideas how to handle Optimistic concurrency exceptions in EF5? Actually even general pointers on exception handling in SaveChanges() would be nice

Thanks

解决方案

The way how to solve concurrency exception in DbContext API reloads original entity:

catch (DbUpdateConcurrencyException ex)
{
    // Get failed entry
    var entry = ex.Entries.Single(...);
    // Overwrite original values with values from database but don't
    // touch current values where changes are held
    entry.OriginalValues.SetValues(entry.GetDatabaseValues());
}

You should also be able to use the mentioned code but you must get ObjectContext instance from your DbContext instance (it is just a wrapper around ObjectContext).

catch (DbUpdateConcurrencyException ex)
{
    var objContext = ((IObjectContextAdapter)context).ObjectContext;
    // Get failed entry
    var entry = ex.Entries.Single(...);
    // Now call refresh on ObjectContext
    objContext.Refresh(RefreshMode.ClientWins, entry.Entity);        
}

You may even try:

objContext.Refresh(RefreshMode.ClientWins, ex.Entries.Select(e => e.Entity));

这篇关于实体框架5.0处理乐观并发异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆