c# - 在 Entity Framework 中使用计算的 DateTime 列

标签 c# entity-framework datetime entity-framework-5 calculated-columns

StoreGeneratedPattern="Computed"timestamp 类型的列上完美运行。但是您不能将时间戳显示为人类可读的形式。

因此您不能向用户显示“此行已在 {TimeStamp} 上修改”,因为您根本无法将 SQL 时间戳解析为 DateTime。

现在我还可以为 DateTime 列创建计算列,这样我就不需要手动设置了吗? myEntity.LastModification = DateTime.Now

更不用说它很容易出错,如果某些用户真的想做坏事,只需更改他的计算机时间即可。

最佳答案

是的,如此处所述: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

摘录:

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

编辑:要使数据库服务器端修改日期更新,请遵循此答案 https://stackoverflow.com/a/8461681/1679310 . Computed 设置将重新加载

关于c# - 在 Entity Framework 中使用计算的 DateTime 列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13875784/

相关文章:

javascript - 如何在 Blazor 中执行客户端 UI 事件

java - 是否有更快的方法将任意字符串与 Java 中的月份名称匹配

c# - 是否可以在 TaskManager 应用程序选项卡中隐藏 winform?

c# - 如何开始使用 OAuth 来保护 Web API 应用程序?

entity-framework - 内存数据库中的 Entity Framework (代码优先)用于单元测试

c# - 返回通用实体列表

c# - 数据网格中的主键始终为零

datetime - Coldfusion 计算帐户过期

php - Symfony2 : DateTime object not working

c# - Entity Framework - 外键约束