我有以下的实体框架代码优先的模型类。我想在创建数据库表时为CreateTime
设置默认值当前时间。但是,下面的代码不能编译,因为设置默认的DateTimeOffset值?
类型“System.DateTimeOffset”不能被声明为const
它是一个更好的办法来设置默认值DateTimeOffset
?
public class MyClass
{
public int Id { get; set; }
public string Title { get; set; }
[DefaultValue(now)]
public DateTimeOffset CreateTime
{
get { return _createTime; }
set { _createTime = value; }
}
private const DateTimeOffset now = DateTimeOffset.Now;
private DateTimeOffset _createTime = now;
}
你有意试图“冻结”现在到它被设置的时刻吗? –
'DateTimeOffset'是具有'Offset'属性的'DateTime'。你不能用'const'来使用它,因为大多数_primitive types_可以与它一起使用。 http://social.msdn.microsoft.com/Forums/vstudio/en-US/9e7f1929-9ea2-487c-a037-628dc3c4fb38/the-type-systemdatetime-cannot-be-declared-const –