EF Core + npgsql DateTime uses windows 10 localization for formatting
EF Core + npgsql DateTime uses windows 10 localization for formatting
I updated my development machine to Windows 10 and tried to add a new Required DateTime property to a class. I created the migration with Add-Migration and a reasonable migration was generated:
{
migrationBuilder.AddColumn<DateTime>(
name: "AskingTime",
table: "Questions",
nullable: false,
defaultValue: new DateTime(2000, 1, 1, 15, 0, 0, 0, DateTimeKind.Unspecified));
}
I tried to apply it with Script-Migration, but it fails with this error:
ALTER TABLE "Questions" ADD "AskingTime" timestamp without time zone NOT NULL DEFAULT TIMESTAMP '2000-01-01 15.00.00';
Npgsql.PostgresException (0x80004005): 22007: invalid input syntax for type timestamp: "2000-01-01 15.00.00"
We tried this with my coworkers machine which still has Windows 7 and it works as expected without errors. The problem was that the Finnish localization in Windows 10 specifies a dot as the separator in time field. I got it working on my machine by changing to English(Swedish) time localization, which uses a colon.
So my final question is, is it possible to force either npgsql or EF Core to correctly parse datetime objects with dots as the time separator?
@Albert I actually realized that I have a DateTime field called "PublishDate" which does not have the "Required" Attribute. That field works correctly, even with the finnish localization settings. I can create and query objects with DateTime fields.
– Ville
Jun 29 at 8:41
1 Answer
1
If the problem occurs only in migrations, then you can specify the default value with HasDefaultValueSql
instead of HasDefaultValue
:
HasDefaultValueSql
HasDefaultValue
builder.Entity<Question>()
.Property(b => b.AskingTime)
.HasDefaultValueSql("'2000-01-01 15:00:00'");
Note: I'm not sure that I specified the date in a correct string format, but you got the idea.
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Is problem related only to migrations? What about regular queries?
– Albert
Jun 29 at 7:16