EF 中 ConcurrencyCheck 属性方法的问题
我在 EF 4.1 中找到了两种对实体进行并发检查的方法:
- 字节数组的 TimeStamp 属性
- 其他类型的 ConcurrencyCheck 属性
第一种非常简单。您只需将字节数组属性标记为时间戳,在数据库中创建附加列,然后瞧...
我对第二种方法有问题。当我标记 LastUpdateDate 属性时,实体框架已开始生成用于并发检查的 SQL 脚本。
Property:
[ConcurrencyCheck]
public DateTime LastUpdateDate { get; set; }
Sql:
select
...
where (([Id] = @3) and ([LastUpdateDate] = @4))
...
@4='value'
但是 EF 没有生成用于更新 LastUpdateDate 值的 sql 脚本?
是否可以说 EF 在并发检查后更新 LastUpdateDate 而无需触发器或类似的操作?
第二个问题: 当您有 LastUpdateDate 属性(属性将显示在 UI 中)之类的内容时,在 EF 中使用并发检查的最佳实践是什么?使用 LastUpdateDate 检查并发性并避免在表中为 TimeStamp 创建附加列是否更好? 创建额外的 TimeStamp 属性并放弃使用 DateTime 属性进行并发检查?
I've found two ways of concurrency checking for my entities in EF 4.1:
- TimeStamp attribute for byte array
- ConcurrencyCheck attribute for another types
The first one is very simple. You just mark byte array property as TimeStamp, create additional column in database and voila...
I've got a problem with the second method. Enity Framework has started generate sql script for concurrency check, when I marked the LastUpdateDate property.
Property:
[ConcurrencyCheck]
public DateTime LastUpdateDate { get; set; }
Sql:
select
...
where (([Id] = @3) and ([LastUpdateDate] = @4))
...
@4='value'
But EF does not generate sql script for updating the value of LastUpdateDate?
Is it possible to say EF to update the LastUpdateDate after concurrency checking without triggers or something like this?
And the second question:
What is the best practice of using concurrency checking in EF when you have something like LastUpdateDate property(property will be displayed in UI)? Is it better to check concurency using LastUpdateDate and avoid creating of addtional column for TimeStamp in your tables or
create additional TimeStamp property and renounce of the using DateTime property for concurrency checking?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否尝试过使用 rowversion (时间戳)而不是 DateTime 数据类型来检查并发性?
我会使用时间戳,因为您确信系统会为您更新它。此外,该值将非常精确。
以下博客文章将为您提供有关如何映射时间戳的更多信息。
第一个展示了如何使用时间戳作为并发检查。
代码第一个具有流畅断言的乐观并发
使用 EF4.1 Code First 和 MVC 3 往返时间戳字段
Have you tried to use a rowversion (timestamp) instead of the DateTime datatype to check for concurency?
I would use the timestamp, because you are sure that the system will update it for you. Further more the value will be very precice.
The following blog posts will give you more information about how to map a timestamp.
The first one shows how to use the timestamp as a concurrency check.
Code First Optimistic Concurrency with Fluent Assertions
Round tripping a timestamp field with EF4.1 Code First and MVC 3