EF CodeFirst:Rails 样式创建和修改的列
使用实体框架 CodeFirst,如何创建一个已创建的日期时间列,每次为该表插入一条记录时,该列都会填充当前时间戳,以及一个修改后的日期时间列,该列每次更新行时都会生成时间戳? Rails 默认情况下会执行此操作,我希望 EF 生成的数据库也有此操作,但事实并非如此。这是可以通过数据注释来完成的事情吗?如果是这样,怎么办?
谢谢!
Using Entity Framework CodeFirst, how do I create a created datetime column that gets populated with the current timestamp everytime a record is inserted for that table, and a modified datetime column that has a timestamp generated evertime a row is updated? Rails does this by default and I was hoping the EF generated database would have this as well, but it doesn't. Is this something that can be done with data annotations? If so, how?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
EF 不支持它。 EF 不会自动为您创建这些列。您必须自行执行以下操作之一:
Created
和Modified
属性。您还必须在应用程序中手动维护这些列 (常见方法是覆盖SaveChanges
并相应地设置值)。Created
的默认约束列以及Modified
列的更新触发器。It is not supported in EF. EF will not create these columns for you automatically. You must do it yourselves by either:
Created
andModified
properties in every entity where you want to maintain these values. You must also manually maintain these columns in your application (common approach is overridingSaveChanges
and set values accordingly).Created
columns and update triggers forModified
columns.