C# 实体框架迁移中的强制日期时间

C# 实体框架迁移中的强制日期时间,c#,entity-framework,migration,C#,Entity Framework,Migration,我正在使用实体框架迁移创建一个数据库,我希望所有DateTime字段都创建为DateTime,而不是默认的DateTime2。我不需要DateTime2的精度,我也不想为每个datetime字段分配一个值,即使我不需要它。只要设置适当的属性即可。要将其映射到nullabe文件,请使用以下属性声明: [Column(DbType="datetime NULL")] public DateTime? YourDateTimeProp {get; set;} 查看以了解更多信息。如上所述,MSDN文

我正在使用实体框架迁移创建一个数据库,我希望所有DateTime字段都创建为DateTime,而不是默认的DateTime2。我不需要DateTime2的精度,我也不想为每个datetime字段分配一个值,即使我不需要它。

只要设置适当的属性即可。要将其映射到nullabe文件,请使用以下属性声明:

[Column(DbType="datetime NULL")]
public DateTime? YourDateTimeProp {get; set;}
查看以了解更多信息。

如上所述,MSDN文档建议使用
datetime2
。使用它没有更多的“麻烦”。但是,如果绝对必须将列作为
datetime
类型,则将此属性添加到属性中:

[Column(DbType="datetime")]
public DateTime? SomeDate { get; set; }
如果您现在习惯于使用
datetime2
,并且不想为该列赋值,那么请将其设置为
可为null
。例如,如果您的属性如下所示:

public DateTime SomeDate { get; set; }
然后将其更改为:

public DateTime? SomeDate { get; set; }

Date
.NET
中的一种新数据类型吗?哎呀,很好。固定的!