EF Core + npgsql DateTime uses windows 10 localization for formatting

2

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?

.net
asp.net-core
entity-framework-core
asked on Stack Overflow Jun 29, 2018 by Ville

1 Answer

0

If the problem occurs only in migrations, then you can specify the default value with HasDefaultValueSql instead of 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.

answered on Stack Overflow Jun 29, 2018 by AlbertK • edited Jun 29, 2018 by AlbertK

User contributions licensed under CC BY-SA 3.0