Conversions to text: a `char` space is lost, `Convert.ToString(bool)` gives `true`, and `string.Join` with a null separator returns NULL

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

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

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

调研方向

Start from NpgsqlStringMethodTranslator.cs (lines 217-225, 274-279) and NpgsqlObjectToStringTranslator.cs (102-107) to pad char values with rpad(x,1); in NpgsqlConvertTranslator.cs (36) use a CASE for bool like bool.ToString. Mimic #4001's rpad fix. Run the issue's repro and confirm the generated SQL matches .NET output (space kept, Convert.ToString(bool) gives True, null separator coalesced).

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

描述

Environment
PostgreSQL 18.6 (postgres:18), session TimeZone Etc/UTC
.NET 10 runtime 10.0.11, Npgsql.EntityFrameworkCore.PostgreSQL 10.0.3, EF Core 10.0.4
.NET 11 runtime 11.0.0-rc.2, Npgsql.EntityFrameworkCore.PostgreSQL 11.0.0-rc.2, EF Core 11.0.0-rc.2
Also Npgsql 10.0.3 on both lines, macOS arm64

The first repro prints the same output on both lines; the IndexOf one differs by line, as shown. main at 150011d has the same code.

LINQ Row .NET PostgreSQL SQL
string.Join(sep, r.Tags), char sep = ' ' {a,b} a b ab array_to_string(r."Tags", @sep, '')
r.Text.PadRight(4, sep), char sep = ' ' abc abc␠ abc rpad(r."Text", 4, @sep)
r.Space.ToString() char column ' ' " " "" r."Space"::text
r.Text.IndexOf(r.Space), 10.x only a b, ' ' 1 0 strpos(r."Text", r."Space") - 1
Convert.ToString(r.Flag) true True true r."Flag"::text
string.Join(r.Separator, r.Tags) separator NULL ab NULL array_to_string(r."Tags", r."Separator", '')
Repro
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;

await using var context = new TestContext();
await context.Database.EnsureDeletedAsync();
await context.Database.EnsureCreatedAsync();

context.Rows.Add(new Row { Tags = ["a", "b"], Space = ' ', Flag = true, Separator = null, Text = "abc" });
await context.SaveChangesAsync();
var row = await context.Rows.AsNoTracking().SingleAsync();
var sep = ' ';

// 1. A space in character(1) is lost when it becomes text
Console.WriteLine($"string.Join(' ' parameter, Tags): .NET {string.Join(sep, row.Tags)}|, database {await context.Rows.Select(r => string.Join(sep, r.Tags)).SingleAsync()}|");
Console.WriteLine($"Text.PadRight(4, ' ' parameter): .NET {row.Text.PadRight(4, sep)}|, database {await context.Rows.Select(r => r.Text.PadRight(4, sep)).SingleAsync()}|");
Console.WriteLine($"Space.ToString(): .NET {row.Space.ToString()}|, database {await context.Rows.Select(r => r.Space.ToString()).SingleAsync()}|");

// 2. bool to text
Console.WriteLine($"Convert.ToString(Flag): .NET {Convert.ToString(row.Flag)}, database {await context.Rows.Select(r => Convert.ToString(r.Flag)).SingleAsync()}");

// 3. string.Join treats a null separator as ""
Console.WriteLine($"string.Join(null separator, Tags): .NET {string.Join(row.Separator, row.Tags)}, database {await context.Rows.Select(r => string.Join(r.Separator, r.Tags)).SingleAsync() ?? "NULL"}");

public class TestContext : DbContext
{
    public DbSet<Row> Rows => Set<Row>();

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        => optionsBuilder
            .UseNpgsql(Environment.GetEnvironmentVariable("Test__Npgsql__DefaultConnection"), o => o.UseNodaTime())
            .LogTo(Console.WriteLine, LogLevel.Information)
            .EnableSensitiveDataLogging();
}

public class Row
{
    public int Id { get; set; }
    public string[] Tags { get; set; } = [];
    public char Space { get; set; }
    public bool Flag { get; set; }
    public string? Separator { get; set; }
    public string Text { get; set; } = "";
}

Output (the | marks the end of each value), the same on both lines:

string.Join(' ' parameter, Tags): .NET a b|, database ab|
Text.PadRight(4, ' ' parameter): .NET abc |, database abc|
Space.ToString(): .NET  |, database |
Convert.ToString(Flag): .NET True, database true
string.Join(null separator, Tags): .NET ab, database NULL
Repro (`IndexOf` with a char column, 10.x)
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;

await using var context = new TestContext();
await context.Database.EnsureDeletedAsync();
await context.Database.EnsureCreatedAsync();

context.Rows.Add(new Row { Text = "a b", Space = ' ' });
await context.SaveChangesAsync();
var row = await context.Rows.AsNoTracking().SingleAsync();
Console.WriteLine($"Text.IndexOf(Space): .NET {row.Text.IndexOf(row.Space)}, database {await context.Rows.Select(r => r.Text.IndexOf(r.Space)).SingleAsync()}");

public class TestContext : DbContext
{
    public DbSet<Row> Rows => Set<Row>();

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        => optionsBuilder
            .UseNpgsql(Environment.GetEnvironmentVariable("Test__Npgsql__DefaultConnection"), o => o.UseNodaTime())
            .LogTo(Console.WriteLine, LogLevel.Information)
            .EnableSensitiveDataLogging();
}

public class Row
{
    public int Id { get; set; }
    public string Text { get; set; } = "";
    public char Space { get; set; }
}

Output:

.NET 10, 10.0.3:        Text.IndexOf(Space): .NET 1, database 0
.NET 11, 11.0.0-rc.2:   Text.IndexOf(Space): .NET 1, database 1
  • char spaces. char columns and parameters are character(1), and casting character(n) to text strips trailing spaces, so ' ' becomes ''. #4001 handled this for IndexOf on main by padding with rpad(x, 1). Three places still lose it: string.Join (NpgsqlStringMethodTranslator.cs#L274-L279), PadLeft/PadRight (NpgsqlStringMethodTranslator.cs#L217-L225) and ToString() (NpgsqlObjectToStringTranslator.cs#L102-L107). #4001 came with the new char-argument translations and isn't on hotfix/10.0.4, so IndexOf(char column) stays wrong on 10.x.
  • Convert.ToString(bool) is ::text (NpgsqlConvertTranslator.cs#L36). bool.ToString() already uses a CASE that gives True/False.
  • string.Join treats a null separator as empty. array_to_string(..., NULL, '') is NULL, so COALESCE(separator, '') would match.
主要语言
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 摘要。