gpt4 book ai didi

entity-framework - Entity Framework : about OptimisticConcurrencyException

转载 作者:行者123 更新时间:2023-12-04 05:02:43 25 4
gpt4 key购买 nike

我有两个相关的表:dbo.resourcesdbo.reservation ; dbo.resources有一个指向 dbo.reservation 的外键与 ON DELETE SET NULL选项。我在 dbo.resources 上也有一个“之后”触发器,即在删除资源时删除保留。

我“设法”提出了 OptimisticConcurrencyException当我尝试手动删除保留和资源时,这看起来很正常。

然后我找到了问题并消除了它,但现在我想通过捕获 OptimisticConcurrencyException 来强制执行更强大的异常处理机制。并刷新上下文。

我的问题是:谁能向我解释为什么这段代码:

 catch (System.Data.OptimisticConcurrencyException ocException)
{
foreach (var objectStateEntry in ocException.StateEntries)
_Context.Refresh(System.Data.Objects.RefreshMode.ClientWins, objectStateEntry.Entity);
_Context.SaveChanges();
}

不起作用,

而这个:
 catch (System.Data.OptimisticConcurrencyException ocException)
{
foreach (var objectStateEntry in ocException.StateEntries)
_Context.Refresh(System.Data.Objects.RefreshMode.StoreWins, objectStateEntry.Entity);
_Context.SaveChanges();
}

吗? EF 在每种情况下(StoreWins 和 ClientWins)如何处理对象上下文? “幕后”究竟发生了什么?

最佳答案

根据引用文章found here :

After the Refresh method is called, the object’s original values will always be updated with the data source value, but the current values might or might not be updated with the data source value. This depends on the RefreshMode. The StoreWins mode means that the object should be updated to match the data source values. The ClientWins value means that only the changes in the object context will be persisted, even if there have been other changes in the data source. To ensure that an object has been updated by data source-side logic, you can call the Refresh method with the StoreWins value after you call the SaveChanges method.

关于entity-framework - Entity Framework : about OptimisticConcurrencyException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15929407/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com