Querying enum properties inside JSONB columns fails when using JsonStringEnumConverter
Les mainteneurs répondent en général sous 1 jour
Personne n'a encore pris cette issue.
Évaluation
- Difficulté
- 4/5
- Temps estimé
- 3-5 jours
- Accessibilité débutants
- 48/100
Piste de recherche
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.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Description
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
- Langage dominant
- C#
- Étoiles
- 1.8k
- Forks
- 268
- Merge moyen
- 2 h 35 min
- PR mergées (30 j)
- 136
Préparer son environnement
Ce projet ne fournit ni conteneur de développement, ni Dockerfile, ni guide de contribution : l'installation est à votre charge. Commencez par son README, et consultez notre guide de la première contribution pour les étapes générales.
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Autres issues de npgsql/efcore.pg
-
Difficulté 2/5 1-3 heures Accessibilité débutants 82/100
Les mainteneurs répondent en général sous 1 jour
-
NTS: the M ordinate is dropped from geometry SQL literals and from geometries stored in JSONPeut-être pris @Ahmed7991 l’a pris il y a 5 jours. Ouverte
Difficulté 2/5 1-3 heures Accessibilité débutants 85/100
npgsql/efcore.pg#4093 · 1 commentaire ·
Les mainteneurs répondent en général sous 1 jour
-
`TimeSpan`/`Duration` component members: negative values are floored, and intervals computed in SQL are read field by fieldPeut-être pris @jeremy-morren l’a pris il y a 11 jours. Ouverte
Difficulté 2/5 1-3 heures Accessibilité débutants 70/100
Les mainteneurs répondent en général sous 1 jour
-
Numeric storage parameters are formatted with the current culture, so migrations emit 0,01 instead of 0.01Peut-être pris @Sorbitol l’a pris il y a 12 jours. Ouverte
Difficulté 2/5 1-3 heures Accessibilité débutants 30/100
Les mainteneurs répondent en général sous 1 jour
-
`Contains` on a JSON primitive collection compares JSON text, so `DateTime` and `TimeOnly` items never matchPeut-être pris @Mr444-dev l’a pris il y a 7 jours. Ouverte
Difficulté 4/5 3-5 jours Accessibilité débutants 45/100
npgsql/efcore.pg#4099 · 1 commentaire ·
Les mainteneurs répondent en général sous 1 jour
Toutes les issues de npgsql/efcore.pg
Issues similaires
-
Difficulté 2/5 1-3 heures Accessibilité débutants 82/100
Les mainteneurs répondent en général sous 1 jour
-
[Feature]: Add Ukrainian (Українська) as a target language for the Translator (Ollama) engineOuverteenhancement
Difficulté 2/5 1-3 heures Accessibilité débutants 73/100
KnifeLemon/Filee#1344 ·
Les mainteneurs répondent en général sous 1 jour
-
Issue-Bug Needs-Triage
Difficulté 2/5 1-3 heures Accessibilité débutants 78/100
Les mainteneurs répondent en général sous 2 jours
-
Difficulté 2/5 1-3 heures Accessibilité débutants 66/100
JiaFeiMiao-K-Cat/DiscImageStudio#281 ·
Les mainteneurs répondent en général sous 1 jour
-
bug triage
Difficulté 2/5 1-3 heures Accessibilité débutants 76/100