EF并发处理与模式第一种方法时间戳属性 [英] EF Concurrency Handling with Timestamp attribute in Model First approach
问题描述
我试图实施<特定的解决方案href="http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/handling-concurrency-with-the-entity-framework-in-an-asp-net-mvc-application"相对=nofollow>处理并发实体框架在ASP.NET MVC应用程序。
文章说:
在模型\ Department.cs,添加一个跟踪属性:
Adding a Tracking Property to the Department Entity
In Models\Department.cs, add a tracking property:
[Timestamp]
public Byte[] Timestamp { get; set; }
时间戳属性指定此列将包括更新的Where子句中,并删除发送到数据库的命令。
The Timestamp attribute specifies that this column will be included in the Where clause of Update and Delete commands sent to the database.
由于我使用的型号第一个的办法,我也跟着步骤1 - 5中的创建一个时间戳列实体框架
Since I'm using a model first approach, I followed steps 1 - 5 outlined in Creating a Timestamp column with Entity Framework
- 添加一个名为时间戳属性的实体EF模型
- 设置类型为二进制
- 设置为空的假
- 设置StoreGeneratedPattern式来计算
- 设置ConcurrencyMode为固定
当我从数据库模型更新我的code时,模型\ Department.cs现在包含
When I update my code from the database model, the Models\Department.cs now contains
public virtual byte[] Timestamp
{
get;
set;
}
然后我用了元数据类指定时间戳属性:
Then I used the metadata class to specify the Timestamp attribute:
// Metadata for Department entity
public class DepartmentMetadata
{
[Timestamp]
public byte Timestamp { get; set; }
}
Q1。我测试是否时间戳值
列正在发生变化的情况下排的编辑。事实并非如此。
Q1. I tested whether the value of the Timestamp
column is changing in case of row edits. It isn't.
修改1 的问题造成的,因为SQL Server列类型是二
,它应该是类型时间戳
。改变数据类型固定timestamp列的问题没有得到更新。
EDIT 1 The problem was caused because the SQL Server column type was binary
, where it should have been of type timestamp
. Changing the data type fixed the issue of the Timestamp column not getting updated.
Q2。我试图修改同一个实体(使用打开新的标签),以查看是否 OptimisticConcurrencyException
被抛出。事实并非如此。我究竟做错了什么?请帮助我了解,谢谢。
Q2. I was trying to modify the same entity (using "Open in new tab") to see if an OptimisticConcurrencyException
is thrown. It isn't. What am I doing wrong? Please help me understand, thanks.
推荐答案
的问题造成的,因为我有每个HTTP会话只有一个ObjectContext的,所以打开一个新的标签和测试,将不会引起并发异常。
The problem was caused because I had one ObjectContext per HTTP session, and so opening a new tab and testing it would NOT cause a concurrency exception.
这篇关于EF并发处理与模式第一种方法时间戳属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!