在使用实体框架时锁定编辑记录的最佳做法 [英] Best practice to lock a record for editing while using entity framework

查看:154
本文介绍了在使用实体框架时锁定编辑记录的最佳做法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不太确定如何说这个问题,但在这里。我正在开展一个项目,其中多个客户端应用程序通过WCF服务访问一个数据源。这可能并不相关,但WCF服务正在利用实体框架来访问此数据源。每当一个客户端查询一个记录进行编辑时,我想防止其他客户端修改相同的记录,直到第一个客户端完成更新。



如果我错了,请纠正我,但我相信这也被称为同步和异步数据访问。



我的问题是,实现此功能的行业最佳做法是什么。有没有办法从数据库端(使用SQL)进行控制,还是必须通过客户端来完成?



我已经考虑为每个表包含一个布尔的EditMode列,只是在编辑时将其设置为true,并检查是否在允许另一个表之前设置为true

解决方案

最佳做法是使用RowVersion和乐观锁定。



乐观并发模式解释。



如果首先使用代码,那么在您的POCO中包含一个字段。

  public virtual byte [ ] RowVersion {get;组; } 

EF将在表中添加一个Timestamp / RowVersion属性。将在更新期间检查。并在数据库更改时自动更新。



编辑:更好地解释。



<什么EF寻找的是属性是并发字段,所以你可以实际上控制一个或多个字段的并发。

  entity.Property(p => p.RowVersion).IsConcurrencyToken()

执行更新或删除您捕获定义的异常

  catch(DbUpdateConcurrencyException ex)

EF将RowVersion视为并发令牌。这是一般使用的方法。由于SQLServer会为您自动更新此字段类型。
那么非常快速和容易。但是您可以告诉EF一个属性是一个并发令牌,并且有多个。



所以EF应该添加属性到where子句进行更新和删除
以确保访问后记录没有更改。


Not quite sure how to word this question but here it goes. I am working on a project where multiple client applications are accessing one data source through a WCF service. It may not be relevant but the WCF service is utilizing entity framework to access this data source. Whenever a client has queried a record for editing, I would like to prevent that same record from being edited by the rest of the clients till the first client has completed their update.

Correct me if I am wrong but I believe this is also known as synchronous and asynchronous data access.

My question is, what is the industry best practice to implement this functionality. Is there a way to control this from the database side (Using SQL) or must it be done through the client?

I have considered including a boolean 'EditMode' column for each table and simply setting it to true when it is being edited and check if that is set to true before allowing another client to access that record.

解决方案

Best practice is to use RowVersion and Optimistic locking.

Optimistic Concurrency Patterns explained.

If using Code first, then include a field in your POCO.

public virtual byte[] RowVersion { get; set; }

EF will add a Timestamp/RowVersion property to your table. It will be checked during Update. And automatically updated by the DB when changed.

EDIT: to better explain.

What EF is looking for is properties that are concurrency fields so you can actually control the concurrency with one or more fields.

entity.Property(p => p.RowVersion).IsConcurrencyToken()

when performing an update or delete you catch the defined exception

catch (DbUpdateConcurrencyException ex)

EF treats the RowVersion as a concurrency token. This is the Generally used approach. Since SQLServer will automatically update this field type for you. So very fast and easy. But you can tell EF a property is a concurrency token explicitly and have more than one.

So EF should add properties to the where clause for updates and deletes to make sure the record didn't change since accessed.

这篇关于在使用实体框架时锁定编辑记录的最佳做法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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