LINQ to SQL:强制更改表的时间戳

发布于 2024-10-26 13:22:25 字数 531 浏览 1 评论 0原文

我正在使用 LINQ to SQL,并且需要一些帮助来更新表中的时间戳列。

Car candidate = Context.Cars.Where(c => c.CarID == car.Id).SingleOrDefault();
candidate.CarName = carToUpdate.CarName;
candidate.CarDescription = carToUpdate.CarDescription;
candidate.IsActive = carToUpdate.IsActive;
candidate.IsCab = carToUpdate.IsCab;
candidate.StockTypeId = carToUpdate.StockTypeId;
Context.SubmitChanges();

如果汽车实体的属性没有变化,则时间戳不会改变。 (看起来 LINQ to SQL 足够智能,不会向数据库发送更新)。

无论如何,有没有办法将时间戳从 LINQ 更改为 SQL?

请帮忙。

谢谢。

I am using LINQ to SQL and require some help in updating a timestamp column in a table.

Car candidate = Context.Cars.Where(c => c.CarID == car.Id).SingleOrDefault();
candidate.CarName = carToUpdate.CarName;
candidate.CarDescription = carToUpdate.CarDescription;
candidate.IsActive = carToUpdate.IsActive;
candidate.IsCab = carToUpdate.IsCab;
candidate.StockTypeId = carToUpdate.StockTypeId;
Context.SubmitChanges();

If there are no changes in the properties of the car entity, the timestamp is not changed.
(Looks like LINQ to SQL is intelligent enough to not send an update to database).

Is there anyway to forefully change timestamp from LINQ to SQL??

Please help.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

装纯掩盖桑 2024-11-02 13:22:25

如何添加 LastUpdatedDate 列并将其设置为 DateTime.Now ,这应该确保它将始终进行更新,然后更改您的时间戳列。

How about adding a LastUpdatedDate column and setting that to DateTime.Now , that should ensure it will always do an update and will then change your timestamp column.

怪我闹别瞎闹 2024-11-02 13:22:25

SQL 表上的触发器不是一个选项吗?

编辑: 我认为该线程回答了您的问题 强制LinqToSql提交

Wouldn't a trigger on the SQL table be an option?

EDIT: I think this thread answers your question force LinqToSql to submit

尬尬 2024-11-02 13:22:25

我想答案是不。

如果修改后实体的属性与将实体从数据库中拉出时的属性相同,则它实际上并未更改。

当行实际上未更新时强制生成新时间戳的目的是什么?

I think the answer is don't.

If the entity's properties are the same after you modify them as they are when you pulled the entity out of the db, it hasn't actually changed.

What's the purpose of forcing a new timestamp to generate when the row was in fact not updated?

開玄 2024-11-02 13:22:25

我正在阅读对象状态和更改跟踪( LINQ to SQL) 并指出:

您可以通过观察来检测更新
变更通知。
通知通过以下方式提供
属性中的 PropertyChanging 事件
设置器。当 LINQ to SQL 收到通知时
第一次更改对象时,它
创建对象的副本并
认为该对象是候选对象
生成更新语句。

对于没有实现的对象
INotifyPropertyChanging、LINQ to SQL
维护值的副本
物体最初时有
物化了。当你打电话时
SubmitChanges、LINQ to SQL 比较
当前值和原始值
判断该对象是否已经存在
改变了。

您是否能够创建一个分部类,添加一个调用 PropertyChanging 的属性来更新未映射且仅存在于代码中的属性?

I was reading through Object States and Change-Tracking (LINQ to SQL) and it states:

You can detect Updates by observing
notifications of changes.
Notifications are provided through the
PropertyChanging event in property
setters. When LINQ to SQL is notified
of the first change to an object, it
creates a copy of the object and
considers the object a candidate for
generating an Update statement.

For objects that do not implement
INotifyPropertyChanging, LINQ to SQL
maintains a copy of the values that
objects had when they were first
materialized. When you call
SubmitChanges, LINQ to SQL compares
the current and original values to
decide whether the object has been
changed.

Would you be able to create a partial class that adds a property that calls PropertyChanging to update a the property that isn't mapped and so only exists in code?

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