Querying enum properties inside JSONB columns fails when using JsonStringEnumConverter
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 初心者へのやさしさ
- 48/100
調査の方向性
Start by running the provided reproduction against PostgreSQL, then read the provider's JSONB query translation and related tests. Trace how the enum property's configured JSON converter affects server-side comparisons; done means the Where query compares the stored string value without an integer cast and the relevant tests pass.
索引モデルが issue の本文から書いたものです。
説明
Description
When a JSONB column contains a property typed as a C# enum, and JsonStringEnumConverter is configured via ConfigureJsonOptions, any server-side query that references the enum property fails with:
Npgsql.PostgresException: 22P02: invalid input syntax for type integer: "Good"
Npgsql generates SQL that casts the extracted JSON value to integer (the enum's backing type) rather than comparing it as text, even though JsonStringEnumConverter is configured and the JSON stores the enum as its string name.
Materializing the full entity via ToListAsync() works fine — the issue is specifically with server-side evaluation of the enum property (e.g. in Where, GroupBy, OrderBy, etc.).
Steps to Reproduce
Minimal reproduction using dotnet run (requires .NET 10+, PostgreSQL):
#:package [email protected]
#:package [email protected]
#:property PublishAot=false
using System.Text.Json;
using System.Text.Json.Serialization;
using Microsoft.EntityFrameworkCore;
var dbContext = new DemoContext(
"Host=localhost;Database=repro_db;Username=postgres;Password=postgres"
);
dbContext.Database.ExecuteSql(
$$"""
drop table if exists parents;
create table parents
(
id integer not null,
data jsonb not null
);
insert into parents (id, data) values
(1, '{"Status": "Good"}'),
(2, '{"Status": "Good"}'),
(3, '{"Status": "Bad"}'),
(4, '{"Status": "Good"}');
"""
);
// This works — client-side deserialization handles string enums correctly
var resultsThatWork = await dbContext.Parents.ToListAsync();
foreach (var result in resultsThatWork)
{
Console.WriteLine(result);
}
// This fails — server-side evaluation casts "Good" to integer
var goodResults = await dbContext.Parents
.Where(x => x.Data.Status == Status.Good)
.ToListAsync();
foreach (var result in goodResults)
{
Console.WriteLine(result);
}
class DemoContext(string connectionString) : DbContext()
{
public DbSet<Parent> Parents => Set<Parent>();
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Parent>(entity =>
{
entity.Property(x => x.Data).HasColumnType("jsonb");
});
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder
.UseNpgsql(
connectionString: connectionString,
optionsBuilder =>
{
optionsBuilder.ConfigureDataSource(dataSourceBuilder =>
{
dataSourceBuilder
.ConfigureJsonOptions(
new JsonSerializerOptions
{
Converters = { new JsonStringEnumConverter() },
}
)
.EnableDynamicJson();
});
}
)
.UseSnakeCaseNamingConvention();
}
}
class Parent
{
public required long Id { get; set; }
public required ThisIsJson Data { get; set; }
}
class ThisIsJson
{
public required Status Status { get; set; }
}
enum Status
{
Good,
Bad,
}
Expected Behavior
The Where clause should generate SQL that compares the extracted JSON value as text, e.g.:
SELECT ... FROM parents AS p WHERE p.data->>'Status' = 'Good'
Actual Behavior
The generated SQL casts the extracted value to integer, causing PostgreSQL to fail:
Npgsql.PostgresException (0x80004005): 22P02: invalid input syntax for type integer: "Good"
Environment
- Npgsql.EntityFrameworkCore.PostgreSQL: 10.0.0
- .NET: 10
- PostgreSQL: 17.5
- 主要言語
- C#
- スター
- 1.8k
- フォーク
- 268
- 平均マージ
- 2時間 35分
- マージ済み PR(30日)
- 136
環境構築
このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
npgsql/efcore.pg のほかの issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
メンテナーはふだん 1 日以内に返信
-
NTS: the M ordinate is dropped from geometry SQL literals and from geometries stored in JSON対応中かも @Ahmed7991 が 5 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 85/100
npgsql/efcore.pg#4093 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
`TimeSpan`/`Duration` component members: negative values are floored, and intervals computed in SQL are read field by field対応中かも @jeremy-morren が 11 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
メンテナーはふだん 1 日以内に返信
-
Numeric storage parameters are formatted with the current culture, so migrations emit 0,01 instead of 0.01対応中かも @Sorbitol が 12 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 30/100
メンテナーはふだん 1 日以内に返信
-
`Contains` on a JSON primitive collection compares JSON text, so `DateTime` and `TimeOnly` items never match対応中かも @Mr444-dev が 7 日前に担当しました。 オープン
難易度 4/5 3〜5日 初心者へのやさしさ 45/100
npgsql/efcore.pg#4099 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
npgsql/efcore.pg の issue をすべて見る
似ている issue
-
Issue-Bug Needs-Triage
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
メンテナーはふだん 2 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 66/100
JiaFeiMiao-K-Cat/DiscImageStudio#281 ·
メンテナーはふだん 1 日以内に返信
-
area/access-management Frontend onboarding candidate :handshake:
難易度 2/5 1〜3時間 初心者へのやさしさ 65/100
Altinn/altinn-auth#4941 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
aws/aws-sdk-net#4586 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
Kryptos-FR/MarkView.Avalonia#269 · リアクション 1 件 ·
メンテナーはふだん 1 日以内に返信