断开连接的 LINQ 更新:行版本与带触发器的日期时间?

发布于 2024-11-06 03:59:52 字数 588 浏览 3 评论 0原文

我们使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

裸钻 2024-11-13 03:59:52

我最近不得不做出类似的决定。

我首先尝试了 rowversion 解决方案。
我发现的缺点:

  • 在LINQ-to-SQL中使用不方便,我将字段映射到byte[]。当你比较字节数组时,代码看起来不太干净
  • 理论上,rowversion可以翻转并再次从0开始,因此具有较高rowversion的行不一定是较旧的行
  • 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:

  • Inconvenient usages in LINQ-to-SQL, I mapped the field to byte[]. The code does not look clean when you compare byte arrays
  • Theoretically rowversion can roll over and start from 0 again, so row with higher rowversion will not necessarily be older row
  • Rowversion is updated on any row update, which in my case was not desirable, I needed to exclude some columns to not affect row version. Having a trigger allows to implement any level of flexibility.

As 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.

猥琐帝 2024-11-13 03:59:52

我倾向于使用时间戳列进行并发检查。一是触发器会对性能产生一些影响,二是对于日期时间列,您将限制自己在 SQL 和 C# 中的日期时间列的精度。

MSDN:

日期时间值四舍五入为 0.000、0.003 或 0.007 秒的增量...

您可能需要查看 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:

datetime values are rounded to increments of .000, .003, or .007 seconds...

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文