【问题标题】:Linq to sql and timestamp fieldLinq to sql 和时间戳字段
【发布时间】:2010-07-26 14:58:56
【问题描述】:
我创建了带有时间戳字段的表。 Allow Nulls 属性设置为 false。
当我想从 repository 更新 entity 时,我得到了该字段的 null 值,但在数据库中,该字段被设置为某个值。
有谁知道可能是什么问题?
我得到了其他字段的正确值。
谢谢。
编辑:
当我从 repository 获取 entity 时,时间戳字段已正确设置。
问题出在http post:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(Product product)
timestamp字段的值在这里丢失了:(
【问题讨论】:
标签:
c#
linq-to-sql
timestamp
【解决方案1】:
默认情况下,VS 生成的编辑视图不包括二进制字段。要在视图中包含时间戳字段,您必须在视图中添加一个隐藏字段,如下所示:
<%: Html.Hidden("Timestamp", Model.Timestamp.ToString()) %>
如果您像我一样遇到编译错误(使用 VS 2010),您还必须将此行添加到您的 Web.config:
<add assembly="System.Data.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
因为 Timestamp.ToString() 是一个扩展方法。
请记住,您不能显式更改数据库中的时间戳字段。 SQL Server 负责管理。