数据库生成的CreatedDate和AmendDate
用户类别:
public class User
{
public DateTime CreatedDate { get; private set; }
public DateTime? AmendDate { get; private set; }
}
我需要列CreatedDate
和AmendDate
具有数据库生成的值(now()
命令)
阅读互联网上的一些文章,建议我在 DbContext
类中的 OnModelCreating
方法中使用它:
Property(p => p.CreatedDate)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
Property(p => p.AmendDate)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Computed);
但是不起作用,说错误 issupported types are rowversion 和时间跨度
。
您如何处理这些属性类型?
有什么建议吗?
User class:
public class User
{
public DateTime CreatedDate { get; private set; }
public DateTime? AmendDate { get; private set; }
}
I need the columns CreatedDate
and AmendDate
have the values generated by the database (now()
command)
Reading some articles on the internet, advised me to use this in DbContext
class on OnModelCreating
method:
Property(p => p.CreatedDate)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
Property(p => p.AmendDate)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Computed);
But does not work, saying that an error is supported types are rowversion and timespan
.
How do you do with these property types?
Any tips?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使两列都可为空,并保存对两列都为空值的更改。您还需要在数据库的相关列中将该列的默认值设置为
getdate()
。然后,如果您尝试插入空值,这些列将自动填充当前日期。但 EF 不会使用数据库生成的日期值更新该属性。其他解决方案是在调用 SaveChanges() 方法之前设置值
You can make both columns nullable and Save changes with both columns with null values. You also need set the default value for the column as
getdate()
in the relevant column in the database. Then these columns will automatically be populated with current date if you try to insert null values. But EF will not update the property with the database generated date value.Other solution is to set the values before you call the
SaveChanges()
method