【问题标题】:EFCore 2.0 default all date fields to use datetime2EFCore 2.0 默认所有日期字段都使用 datetime2
【发布时间】:2017-06-06 14:43:56
【问题描述】:

在 EF6 中,我可以使用以下代码,以便我的所有日​​期都使用 datetime2(0) 作为列类型

modelBuilder.Properties<DateTime>()
    .Configure(c => c
    .HasColumnType("datetime2")
    .HasPrecision(0));

我如何在 EFCore 中做到这一点??

【问题讨论】:

标签: c# .net entity-framework asp.net-core entity-framework-core


【解决方案1】:

您需要安装 Nuget 包:Microsoft.EntityFrameworkCore.SqlServer。安装后,您可以在属性中使用 HasColumnType("datetime")。 :)

【讨论】:

  • 谢谢,这拯救了我的一天。如果您使用的是属性,则为 [Column(TypeName = "datetime")]
  • 这并不像 OP 中指定的那样默认用于同一时间的所有列。您必须为每一列配置它
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-14
相关资源
最近更新 更多