Numeric storage parameters are formatted with the current culture, so migrations emit 0,01 instead of 0.01

未关闭
#4,114 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

@Sorbitol 已经在做这个了。

开始于 2026年10月29日。

  • #4115 来自 @Sorbitol —— 未关闭

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
30/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
停滞
技术栈
csharp, postgresql
领域
databases

调研方向

Start in src/EFCore.PG/Migrations/NpgsqlMigrationsSqlGenerator.cs, at GetStorageParameters, where non-boolean and non-string values are formatted. Check the migration SQL generator tests for storage parameters and add coverage for a decimal value under a comma-decimal culture. Done means generated CREATE TABLE, CREATE INDEX, and ALTER TABLE storage parameter SQL uses a dot decimal separator; pull request #4115 is already open.

由索引模型根据 Issue 内容生成。

描述

Description

A non-integer numeric storage parameter (double, float, decimal) is written into DDL with the current culture's decimal separator. Under sv-SE, de-DE, fr-FR and other comma cultures, HasStorageParameter("autovacuum_vacuum_scale_factor", 0.01) generates autovacuum_vacuum_scale_factor=0,01, which breaks CREATE TABLE … WITH (…) and ALTER TABLE … SET (…) (and CREATE INDEX … WITH (…), which shares the same helper): the statement fails with a syntax error.

Model Culture Generated SQL
HasStorageParameter("autovacuum_vacuum_scale_factor", 0.01) en-US …(autovacuum_vacuum_scale_factor=0.01) ✅
same sv-SE …(autovacuum_vacuum_scale_factor=0,01) ❌
Environment
  • PostgreSQL 18.6 (only to confirm the error; the repro itself needs no database)
  • .NET SDK 10.0.403 / runtime 10.0.14, Npgsql.EntityFrameworkCore.PostgreSQL 10.0.3, EF Core 10.0.4, Npgsql 10.0.3
  • Windows 11, current culture sv-SE
Repro
using System.Globalization;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;

foreach (var culture in new[] { "en-US", "sv-SE" })
{
    CultureInfo.CurrentCulture = new CultureInfo(culture);
    Console.WriteLine($"--- {culture} ---");

    using var context = new TestContext();
    var generator = context.GetService<IMigrationsSqlGenerator>();
    var differ = context.GetService<IMigrationsModelDiffer>();
    var model = context.GetService<IDesignTimeModel>().Model.GetRelationalModel();

    var operations = differ.GetDifferences(null, model);
    foreach (var command in generator.Generate(operations, context.Model))
        Console.WriteLine(command.CommandText);
}

public class TestContext : DbContext
{
    protected override void OnConfiguring(DbContextOptionsBuilder options)
        => options.UseNpgsql("Host=unused");

    protected override void OnModelCreating(ModelBuilder modelBuilder)
        => modelBuilder.Entity<Row>().HasStorageParameter("autovacuum_vacuum_scale_factor", 0.01);
}

public class Row
{
    public int Id { get; set; }
}

Output (Npgsql.EntityFrameworkCore.PostgreSQL 10.0.3):

--- en-US ---
CREATE TABLE "Row" (
    "Id" integer GENERATED BY DEFAULT AS IDENTITY,
    CONSTRAINT "PK_Row" PRIMARY KEY ("Id")
)
WITH (autovacuum_vacuum_scale_factor=0.01);

--- sv-SE ---
CREATE TABLE "Row" (
    "Id" integer GENERATED BY DEFAULT AS IDENTITY,
    CONSTRAINT "PK_Row" PRIMARY KEY ("Id")
)
WITH (autovacuum_vacuum_scale_factor=0,01);

Diffing a model with 0.2 against one with 0.01 gives, under sv-SE: ALTER TABLE "Row" SET (autovacuum_vacuum_scale_factor=0,01); (en-US: =0.01). de-DE and fr-FR produce the same 0,01.

PostgreSQL 18 rejects the sv-SE statement:

ERROR:  syntax error at or near "01"
LINE 5: WITH (autovacuum_vacuum_scale_factor=0,01);
                                               ^
Source reference

GetStorageParameters formats every value other than bool and string with a plain ToString():

https://github.com/npgsql/efcore.pg/blob/cd8b7f9ac6e37deb2bd7ea18d318b6c25fd1b5c1/src/EFCore.PG/Migrations/NpgsqlMigrationsSqlGenerator.cs#L2280-L2290

Its result is used by WITH (…) (emitted for both CREATE TABLE and CREATE INDEX) and by ALTER TABLE … SET (…), while other numeric DDL in the same file already uses CultureInfo.InvariantCulture (RESTART WITH, INCREMENT BY). A likely fix is Convert.ToString(a.Value, CultureInfo.InvariantCulture)! on line 2289.

(Links are to main; the code is identical in v10.0.3, L2066–L2075, and still present on hotfix/10.0.4.)

Similar to #787 (invariant culture for DateTime literals).

Workaround: pass the value as a string ("0.01"), which the generator quotes and PostgreSQL accepts.

主要语言
C#
星标
1.8k
派生
268
平均合并
2 小时 35 分钟
30 天内合并 PR
136

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

npgsql/efcore.pg 的其他 Issue

查看 npgsql/efcore.pg 的全部 Issue

相似的 Issue

更多 C# Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。