简体   繁体   English

在Entity Framework中使用计算的DateTime列

[英]Using computed DateTime columns in Entity Framework

StoreGeneratedPattern="Computed" works flawlessly on columns of type timestamp . StoreGeneratedPattern="Computed"timestamp类型的列上完美运行。 But you can't display a TimeStamp as a human-readable form. 但是你不能将TimeStamp显示为人类可读的形式。

So you can't show the user "This row was modified on {TimeStamp} " because you simply can't parse a SQL Timestamp to DateTime. 因此,您无法向用户显示“此行已在{TimeStamp}上修改”,因为您无法将SQL时间戳解析为DateTime。

Now can I also create computed columns for DateTime columns so I don't need to set it manually? 现在我还可以为DateTime列创建计算列,所以我不需要手动设置它吗? myEntity.LastModification = DateTime.Now

Not to mention it would be quite error-prone, if some user really wanted to do something bad, by simply changing his computers time. 更不用说它会非常容易出错,如果有些用户真的想做坏事,只需改变他的电脑时间。

Yes, as described here: Is there a way to specify default values for columns? 是的,如下所述: 有没有办法为列指定默认值? Some DateTime columns in my database are set to getdate(), but the EF is unaware of these default values 我的数据库中的一些DateTime列设置为getdate(),但EF不知道这些默认值

Extract: 提取:

...If you set the StoreGeneratedPattern to Identity (if the value is only computed on insert) or Computed (if it is computed on every update), then when you call SaveChanges the EF will get the value generated in the database and bring it back to update your object... ...如果将StoreGeneratedPattern设置为Identity(如果值仅在插入时计算)或Computed(如果在每次更新时计算),则当您调用SaveChanges时,EF将获取在数据库中生成的值并将其带入回来更新你的对象......

EDIT: to make the DB server side Modification date updated, follow this answer https://stackoverflow.com/a/8461681/1679310 . 编辑:要更新数据库服务器端修改日期,请按照此答案https://stackoverflow.com/a/8461681/1679310 Computed setting will do the reload Computed设置将执行重新加载

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM