Querying enum properties inside JSONB columns fails when using JsonStringEnumConverter
Maintainer thường phản hồi trong vòng 1 ngày
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức phù hợp với người mới
- 48/100
Hướng nghiên cứu
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.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
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
- Ngôn ngữ chính
- C#
- Star
- 1.8k
- Fork
- 268
- Merge trung bình
- 2 giờ 35 phút
- Pull request đã merge (30 ngày)
- 136
Chuẩn bị môi trường
Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của npgsql/efcore.pg
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
Maintainer thường phản hồi trong vòng 1 ngày
-
NTS: the M ordinate is dropped from geometry SQL literals and from geometries stored in JSONCó thể đã có người làm @Ahmed7991 đã nhận 5 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 85/100
npgsql/efcore.pg#4093 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
`TimeSpan`/`Duration` component members: negative values are floored, and intervals computed in SQL are read field by fieldCó thể đã có người làm @jeremy-morren đã nhận 11 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Numeric storage parameters are formatted with the current culture, so migrations emit 0,01 instead of 0.01Có thể đã có người làm @Sorbitol đã nhận 12 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 30/100
Maintainer thường phản hồi trong vòng 1 ngày
-
`Contains` on a JSON primitive collection compares JSON text, so `DateTime` and `TimeOnly` items never matchCó thể đã có người làm @Mr444-dev đã nhận 7 ngày trước. Đang mở
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 45/100
npgsql/efcore.pg#4099 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của npgsql/efcore.pg
Issue tương tự
-
Issue-Bug Needs-Triage
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
Maintainer thường phản hồi trong vòng 2 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 66/100
JiaFeiMiao-K-Cat/DiscImageStudio#281 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
area/access-management Frontend onboarding candidate :handshake:
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 65/100
Altinn/altinn-auth#4941 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
aws/aws-sdk-net#4586 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
Kryptos-FR/MarkView.Avalonia#269 · 1 reaction ·
Maintainer thường phản hồi trong vòng 1 ngày