By default […] Object Services saves object
changes to the database without
checking for concurrency. For
properties that might experience a
high degree of concurrency,we
recommend that the entity property be
defined in the conceptual layer with
an attribute of
ConcurrencyMode=”fixed”
我有两个问题:
>在我的模型中没有ConcurrencyMode =“fixed”的属性,我可以安全地假设在保存更改时抛出了一个OptimisticConcurrencyException,这是因为实体不再存在于数据存储中,即它已被删除另一个用户,还是我错过了什么?
我想EF会执行一个看起来像这样的UPDATE语句,正如我所看到的,如果ID = 1的Person不存在,它只会引发一个OptimisticConcurrencyException:
UPDATE Person SET FirstName = 'John' AND LastName = 'Smith' WHERE ID = 1
>使用ConcurrencyMode =“fixed”时,EF还会在删除实体时检查并发性吗?换句话说,EF是否会执行看起来像这样的DELETE语句(不仅仅是WHERE子句中的主键):
DELETE FROM Person WHERE ID = 1 AND LastName = 'Doe'
解决方法
(1)是的,但不幸的是,这并不是那么简单.因为EF(3.5)具有独立的关联模型,所以关联也是独立处理的,即使你没有这样说,它也会成为UPDATES和DELETES期间并发检查的一部分.
即,当您更新Person时,您经常会看到如下所示的更新:
UPDATE Person SET Partner = NULL AND FirstName = 'John' AND LastName = 'Smith' WHERE ID = 1 AND Partner = 2
即Partner是FK专栏.
如果你使用FK关联,这一切都会在4.0中发生变化,正如我们所期望的那样.
(2)对于DELETE,在删除期间检查任何ConcurrencyMode =’fixed’属性.例外情况是当您有一个不接受该并发值的SPROC for delete时.
希望这可以帮助
亚历克斯