7

In EF6 i could use the following code so that all my dates use datetime2(0) as the column type

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

How can i do this in EFCore??

Gillardo
  • 9,518
  • 18
  • 73
  • 141
  • 1
    See https://stackoverflow.com/questions/43277154/entity-framework-core-setting-the-decimal-precision-and-scale-to-all-decimal-p/43282620#43282620 – ErikEJ Jun 06 '17 at 16:19
  • 2
    Possible duplicate of [Entity Framework Core - setting the decimal precision and scale to all decimal properties](https://stackoverflow.com/questions/43277154/entity-framework-core-setting-the-decimal-precision-and-scale-to-all-decimal-p) – ErikEJ Jun 06 '17 at 16:20
  • Not having the correct column type can lead to this error: `Conversion failed when converting date and/or time from character string`. That's how I found this question. – Tom Robinson Apr 15 '19 at 15:52

1 Answers1

5

You need to install the Nuget Package: Microsoft.EntityFrameworkCore.SqlServer. After install, you can use HasColumnType("datetime") in your property. :)

Rafael Botelho
  • 398
  • 5
  • 11