断开连接的 LINQ 更新:行版本与带触发器的日期时间?
我们使用 LINQ to SQL 和 WCF 作为新的中间层,并且使用数据传输对象来进行线路传递,而不是使用实际的 LINQ 类。我将使用此处概述的一种或另一种方法 - Linq Table Attach() 基于时间戳或行版本 - 以确保更新正确工作并正确处理并发。
为了节省大家的阅读时间,本质上,您可以在表中使用时间戳/行版本列,或者使用带有默认值和更新触发器的日期时间列 - 无论哪种方式,它都会为您提供一个每次都会获取新生成的值的列。发生插入或更新时,该列是 LINQ 用于检查并发性的列。
我的问题是 - 哪一个更好?我们的许多表中已经有“UpdatedWhen”的日期时间列(但不是全部 - 不要问),但会添加默认值和触发器,或者我们可以只添加 rowversion (我们必须使用现在,我们仍然支持 SQL2005 一段时间)到每个表 - 无论哪种方式,我们都会修改数据库以使其正常工作,所以我想知道是否存在性能差异或任何差异这两种选择之间需要注意的其他重要区别。我尝试在网上和这里搜索,但到目前为止还没有运气。谢谢。
We're using LINQ to SQL and WCF for a new middle tier, and we're using Data Transfer Objects for passing over the wire rather than using the actual LINQ classes. I'm going to be using one or the other of the methods outlined here - Linq Table Attach() based on timestamp or row version - in order to ensure that updates work correctly and that concurrency is handled correctly.
To save you folks some reading time, essentially you can either use a timestamp/rowversion column in your table or have a datetime column with a default and an update trigger - either way it gets you a column that gets a newly generated value each time an insert or update occurs, and that column is the one used by LINQ to check for concurrency.
My question is - which one is better? We already have datetime columns for "UpdatedWhen" in many of our tables (but not all - don't ask), but would be adding in the defaults and the triggers, or we could just add the rowversion (we'd have to use the timestamp syntax for now, since we're still supporting SQL2005 for a while) to each table - either way we're modifying the DB in order to make it work, so I'd like to know whether there's a performance difference or any other important difference to note between these two alternatives. I've tried searching the web and here on SO, but no luck so far. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我最近不得不做出类似的决定。
我首先尝试了 rowversion 解决方案。
我发现的缺点:
因此,我使用带有默认约束和更新触发器的 datetime2 列将值设置为 sysutcdatetime()
。
此类型的精度为 100 纳秒(精度 7 位 - 23:59:59.9999999)
。
尽管这是可能的,但我还从未见过两次相同值的生成。但就我而言,如果有重复项也不会造成伤害。如果这对我很重要,我会添加唯一约束,看看这是否会失败。
我使用 sysutcdatetime() 因为这个值不会受到夏令时的影响。
I had to make the similar decision recently.
I tried rowversion solution first.
The disadvantages that I found:
rowversion
can roll over and start from 0 again, so row with higherrowversion
will not necessarily be older rowAs a result I used datetime2 column with a default constraint and update trigger to set the value to sysutcdatetime()
.
This type has accuracy 100 nanoseconds (precision 7 digits - 23:59:59.9999999)
.
Although it is possible, I never saw generation of the same value twice yet. But in my case it will not hurt if there will be duplicates. If it was important to me, I would add unique constraint, and see if this ever fails.
I used sysutcdatetime() as this value would not be affected by daylight saving.
我倾向于使用时间戳列进行并发检查。一是触发器会对性能产生一些影响,二是对于日期时间列,您将限制自己在 SQL 和 C# 中的日期时间列的精度。
MSDN:
您可能需要查看 SO:C#中的DateTime和SQL Server中的DateTime之间有什么区别吗?和MSDN:日期时间 (Transact-SQL) 了解详细信息。
I would lean towards using timestamp column for concurrency checks. One - triggers would have some impact upon performance and two - with date time column you'll be limiting yourself to the precision of DateTime column in SQL and C#.
MSDN:
You may want to look at SO: Is there any difference between DateTime in c# and DateTime in SQL server? and MSDN: datetime (Transact-SQL) for more info.