EFCore 2.0 默认所有日期字段使用 datetime2

Gil*_*rdo 7 .net c# entity-framework entity-framework-core asp.net-core

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

modelBuilder.Properties<DateTime>()
    .Configure(c => c
    .HasColumnType("datetime2")
    .HasPrecision(0));
Run Code Online (Sandbox Code Playgroud)

我怎样才能在 EFCore 中做到这一点?

Raf*_*lho 5

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

  • 这并不像OP中指定的那样默认用于同一时间的所有列。您必须为每一列配置它 (4认同)
  • 谢谢,这救了我的命。如果您使用属性,则为“[Column(TypeName = "datetime")]” (2认同)