简体   繁体   English

实体框架的悲观并发

[英]Pessimistic Concurrency for Entity Framework

In my project, users will be able to edit some sets of data as much as needed. 在我的项目中,用户可以根据需要编辑一些数据集。 However, due to the project specification, only one user can edit a certain data set/ access the data set's edit page at a time. 但是,由于项目规范,一次只能有一个用户编辑某个数据集/访问数据集的编辑页面。 So if user A is editing Data Set 1 , user B must not be able to access the edit page of Data Set 1 so long as user A is at that page, and vice versa. 因此,如果用户A正在编辑数据集1 ,则只要用户A在该页面上, 用户B就不能访问数据集1的编辑页面,反之亦然。

I did a bit of reading and Pessimistic Concurrency seems to be what I'm looking for, where a page gets "locked" out until the user inside that page leaves, which explains my title. 我做了一些阅读和Pessimistic Concurrency似乎是我正在寻找的,一个页面被“锁定”,直到该页面内的用户离开,这解释了我的标题。

I am still fairly new to ASP.NET and web development in general. 我仍然是ASP.NET和Web开发的新手。 I would like to ask if I'm heading in the right direction to approach my problem, How could I implement it in my project (I only keep seeing samples for Optimistic Concurrency) and if there are other ways, which I have not encountered, To approach my problem. 我想问一下我是否朝着正确的方向前进我的问题,我怎样才能在我的项目中实现它(我只看到乐观并发的样本)以及是否有其他方法,我没有遇到过,解决我的问题。

First of all I'd say that your question is about the database and EF, not ASP.NET MVC. 首先,我要说的是你的问题是关于数据库和EF,而不是ASP.NET MVC。 By the way, There are two basic types of concurrency control: 顺便说一下,有两种基本类型的并发控制:

Pessimistic concurrency which locks data between the time that requested by a user and a time he saves changes so that no other users can save changes the same data during that time. 悲观的并发性将数据锁定在用户请求的时间和他保存的时间之间进行更改,以便其他用户无法在此期间保存更改相同的数据。

Optimistic concurrency which locks nothing and assumes that the second user is attempted to change the data won't conflict with changes saved by the first user, but if there is a conflict the second user's changes are aborted and only the first user's changes remain. 没有锁定并假设第二个用户试图更改数据的乐观并发不会与第一个用户保存的更改冲突,但如果存在冲突,则第二个用户的更改将中止,并且仅保留第一个用户的更改。
EF doesn't support Pessimistic concurrency directly. EF不直接支持悲观并发。

So I think the best option for you is Optimistic concurrency which EF does support, which as you probably know, it works by inspecting pre- and post-update data to determine if user B modified it between the time user A retrieved it and updated it. 所以我认为最好的选择是EF支持的乐观并发,正如您可能知道的那样,它通过检查更新前和更新后的数据来确定用户B是否在用户A检索它并更新它之间对其进行了修改。 If it changed then user A's modifications are discarded, If not then user A's modifications are updated into the database. 如果它改变了,则丢弃用户A的修改,如果没有,则用户A的修改被更新到数据库中。

To implement Optimistic concurrency you can simply define a RowVersion property for your model: 要实现Optimistic并发,您只需为模型定义RowVersion属性:

public byte[] RowVersion { get; set; }

and in your model's configuration you should tell EF that it has the behaviour of a Row Version in property: 在您的模型配置中,您应该告诉EF它在属性中具有行版本的行为:

Property(p => p.RowVersion).IsRowVersion();

Now if user A retrieves the data, and user B retrieves same data and user B saves the changes before user A saves his changes, user A's changes are discarded and an exception is thrown: 现在,如果用户A检索数据,并且用户B检索相同数据并且用户B在用户A保存其更改之前保存更改,则用户A的更改将被丢弃并抛出异常:

Store update, insert, or delete statement affected an unexpected number of rows (0). 存储更新,插入或删除语句会影响意外的行数(0)。 Entities may have been modified or deleted since entities were loaded. 自实体加载后,实体可能已被修改或删除。 Refresh YourModelName entries. 刷新YourModelName条目。

More info . 更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM