Skip to content

Invariant culture is not used for DateTime in GenerateNonNullSqlLiteral #787

Closed

Description

@ljani

Consider following migration:

using System;
using Microsoft.EntityFrameworkCore.Migrations;

namespace MvcMovie.Migrations
{
    public partial class F2 : Migration
    {
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.AddColumn<DateTime>(
                name: "Test",
                table: "Movie",
                nullable: false,
                defaultValue: new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified));
        }

        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropColumn(
                name: "Test",
                table: "Movie");
        }
    }
}

If I set my computer locale to Finnish, Npgsql will try to execute the following SQL statement, which fails:

ALTER TABLE "Movie" ADD "Test" timestamp without time zone NOT NULL DEFAULT TIMESTAMP '0001-01-01 00.00.00'

(It's the dots which fail, '0001-01-01 00:00:00' does work.)

The DateTime is correctly formatted by SQL Server provider:

ALTER TABLE [Movie] ADD [Test] datetime2 NOT NULL DEFAULT '0001-01-01T00:00:00.0000000'

In other words, the same migration works with SQL Server.

The only difference I was able to spot is that Sql Server provider overrides SqlLiteralFormatString (and this) whereas Npgsql is overriding GenerateNonNullSqlLiteral. I didn't have the time to look further into this issue. Do you think this could be the problem?

EDIT: Fix links.

Activity

  1. ljani commented on Jan 8, 2019

    @ljani
    Author

    That is probably the issue, because RelationalTypeMapping passes CultureInfo.InvariantCulture to string.Format in GenerateNonNullSqlLiteral whereas Npgsql does not do that.

    EDIT: Here's a repro:

    Microsoft (R) Roslyn C# Compiler version 2.10.0.0
    Loading context from 'CSharpInteractive.rsp'.
    Type "#help" for more information.
    > $"TIMESTAMP '{(DateTime)(new DateTime(1, 1, 1, 1, 1, 1)):yyyy-MM-dd HH:mm:ss.FFFFFF}'"
    "TIMESTAMP '0001-01-01 01.01.01'"
    

    PS. @roji feel free to fix this issue as you see it is done best :)

  2. self-assigned this
    on Jan 8, 2019
  3. austindrenski commented on Jan 8, 2019

    @austindrenski
    Contributor

    @ljani Thanks for reporting this to us. It does appear that we need to use invariant culture formatting for date and time types.

    Note that this does not impact the NodaTime plugin, because those type mappings use NodaTime-provided formatters which format with the invariant culture by default.

  4. changed the title [-]Incorrect ALTER clause is generated with a DateTime as a default value with Finnish locale[/-] [+]Invariant culture is not used for DateTime in GenerateNonNullSqlLiteral[/+] on Jan 8, 2019
  5. modified the milestones: , 2.2.1 on Jan 8, 2019
  6. ljani commented on Jan 9, 2019

    @ljani
    Author

    @austindrenski Great, thanks for the speedy fix!

  7. added theissue type on Jun 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions