问题描述
当用户对某个实体打开编辑表格,我想锁定该实体,并让她做任何修改。在编辑她需要,以确保没有其他人在这日编辑操作。
when user opens edit form for some entity, I would like to LOCK this entity and let her make any changes. During editing she needs to be sure that nobody else does any edit operations on it.
我如何可以锁定在实体框架(C#)实体4+,数据库MS SQL Server 2008的?
How can I lock an entity in Entity Framework (C#) 4+, database MS SQL Server 2008?
感谢你这么多提前!
推荐答案
有两种方法来处理这些情况:
There are two ways to handle these situations:
-
开放式并发,你允许并发编辑和插入和捕获异常,如果一些违反并发规则。乐观并发是唯一约束守着同一个项目的插入和时间戳/行版本列守着并发更新相同的项目执行。如果别人更新行时当前用户正在改变的应用程序将引发
OptimisticConcurrencyException
保存过程中,你将不得不让用户既可以覆盖其他的改变或重新加载新的存储的数据。
Optimistic concurrency where you allow concurrent edits and inserts and catch exception if something violates concurrency rules. Optimistic concurrency is enforced by unique constraints guarding inserts of the same items and by timestamps / row version columns guarding concurrent updates to the same item. If somebody else updates row when current user is making changes the application will throw
OptimisticConcurrencyException
during saving and you will have to allow user to either overwrite other changes or reload new stored data.
悲观并发其中记录已被锁定,由任何客户preventing其他客户更新同一记录所执行的操作的持续时间。悲观并发通常是通过强制添加到您的表像 LockedBy
, LockedAt
自定义列等,一旦这些列都充满没有其他人可以选择进行编辑记录。 LockedAt
可以帮你实现锁发出一些自动失效。长期运行的EF交易没有长时间运行的数据库事务。
Pessimistic concurrency where the record is locked for the duration of the operation executed by any client preventing other clients to update the same record. Pessimistic concurrency is usually enforced by custom columns added to your tables like LockedBy
, LockedAt
, etc. Once these columns are filled nobody else can select the record for edit. LockedAt
can help you implement some automatic expiration of issued locks. Long running "EF transactions" are not long running database transactions.
您最初的描述导致了第二个方案这是有道理的在某些应用中。
Your initial description leads to second scenario which makes sense in some applications.
这篇关于长时间运行实体框架的交易的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!