LINQ to SQL:强制更改表的时间戳
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如何添加 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.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
我想答案是不。
如果修改后实体的属性与将实体从数据库中拉出时的属性相同,则它实际上并未更改。
当行实际上未更新时强制生成新时间戳的目的是什么?
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?
我正在阅读对象状态和更改跟踪( LINQ to SQL) 并指出:
您是否能够创建一个分部类,添加一个调用
PropertyChanging
的属性来更新未映射且仅存在于代码中的属性?I was reading through Object States and Change-Tracking (LINQ to SQL) and it states:
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?