微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – EF 7为DateTime列设置初始默认值

我需要能够设置DateTime列的初始值.

当我指定“getutcdate()或DateTime.UtcNow

entity.Property(e => e.ShipDate).DefaultValue("getutcdate()")
entity.Property(e => e.ShipDate).DefaultValue(DateTime.UtcNow)

然后运行dnx. ef migration add InitialMigration EF使用以下命令生成迁移快照:

b.Property<DateTime?>("ShipDate")
  .required()
  .Annotation("Relational:ColumnDefaultValue","getutcdate()")
  .Annotation("Relational:ColumnDefaultValueType","System.String");

当我使用DateTime.UtcNow

b.Property<DateTime?>("ShipDate")
  .Annotation("Relational:ColumnDefaultValue","635754129448768827")
  .Annotation("Relational:ColumnDefaultValueType","System.DateTime");

和初始迁移:

ShipDate = table.Column(
  type: "datetime2",nullable: false,defaultValue: "getutcdate()"),

当我使用DateTime.UtcNow

ShipDate = table.Column(
  type: "datetime2",nullable: true,defaultValue: new DateTime(2015,8,17,12,55,44,876,DateTimeKind.Unspecified)),

看起来它必须工作,但是当我将数据插入表中时,列的认值是创建时间戳的“即时”.

我错过了什么吗?

另外,既然如此,如何在EF7中指定IDENTITY SEED?

谢谢

UPDATE
生成sql脚本后,我得到两个选项:

如果使用“getutcdate()”:

[ShipDate] datetime2 DEFAULT 'getutcdate()',

由于引号不起作用

或者如果使用DateTime.utcNow

[ShipDate] datetime2 DEFAULT '2015-08-17 12:55:44.8760000'

这解释了我得到的静态价值.

我想我可以用这个来管理.
这是一个错误还是有正确的方法吗?
谢谢

解决方法

您想要设置认值sql,而不是常量值:
entity.Property(e => e.ShipDate).HasDefaultValuesql("getutcdate()");

原文地址:https://www.jb51.cc/csharp/98921.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐