You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Using NpgsqlParameter in SqlQuery can generate invalid query if parameter is referenced multiple times in v10 #3703
Attempting to use NpgsqlParameter with SqlQuery explicitly in v10 can generate invalid queries if the same parameter instance is referenced multiple times.
The same issue does not occur using v9.
Consider the following example:
varv=newNpgsqlParameter<int>("value",1);varvalues=awaitdbContext.Database.SqlQuery<int>($""" SELECT 1 WHERE {v} >= 0 OR {v} <= 0""").ToListAsync();
where we want to ensure parameter value is provided once and used two times in the query.
Using Npgsql.EntityFrameworkCore.PostgreSQL 10.0.0 the generated log is the following
where a single parameter is provided and the same placeholder is used twice.
It seems both v9 and v10 recognize the parameter instance as unique and provide a single parameter to the SQL command, but v10 does not use the same placeholder for each usage.
Upon further inspection and debugging, the issue is not specifically related to Npgsql and likely addressed by dotnet/efcore#37430. @roji are there any plans to include the fix in a servicing release for .NET 10? I tend to use a parameter instance explicitly to avoid to provide the same value multiple times (expecially in case it is provided as json) or wrap a value type to make explicit that a certain value is the same in multiple places of a query.
@BladeWise can you please give this a try with the recently-released 10.0.2, which contains many fixes around duplicate parameter handling? When I looked at dotnet/efcore#37430, it seemed to be triggered specifically by using GroupBy in a very specific configuration, which your repro does not do.
Thanks for the reply @roji.
The repro already uses 10.0.2, it is imported as a transient dependency through Microsoft.EntityFrameworkCore.Design 10.0.2, but to be sure I repeated the test with an explicit dependency on Microsoft.EntityFrameworkCore.Relational 10.0.2 and the issue presists.
I did notice your comment regarding the specific scenario, but as you can see my repro is quite basic, and seems to point out that explicit usage of the same parameter multiple times triggers the problem even in 10.0.2.
Thanks, I can indeed see this happening - see minimal repro below. As this indeed probably represents a much more common scenario, I'll look at backporting dotnet/efcore#37430 to 10.0.x.
Minimal repro produced with AI
awaitusingvarcontext=newBlogContext();awaitcontext.Database.EnsureDeletedAsync();awaitcontext.Database.EnsureCreatedAsync();varv=newNpgsqlParameter<int>("value",1);varvalues=awaitcontext.Database.SqlQuery<int>($""" SELECT 1 WHERE {v} >= 0 OR {v} <= 0""").ToListAsync();Console.WriteLine($"Result count: {values.Count}");publicclassBlogContext:DbContext{protectedoverridevoidOnConfiguring(DbContextOptionsBuilderoptionsBuilder)=>optionsBuilder.UseNpgsql(Environment.GetEnvironmentVariable("Test__Npgsql__DefaultConnection")).LogTo(Console.WriteLine,LogLevel.Information).EnableSensitiveDataLogging();}
Activity
BladeWise commentedon Jan 17, 2026
Upon further inspection and debugging, the issue is not specifically related to Npgsql and likely addressed by dotnet/efcore#37430.
@roji are there any plans to include the fix in a servicing release for .NET 10? I tend to use a parameter instance explicitly to avoid to provide the same value multiple times (expecially in case it is provided as json) or wrap a value type to make explicit that a certain value is the same in multiple places of a query.
roji commentedon Jan 19, 2026
@BladeWise can you please give this a try with the recently-released 10.0.2, which contains many fixes around duplicate parameter handling? When I looked at dotnet/efcore#37430, it seemed to be triggered specifically by using GroupBy in a very specific configuration, which your repro does not do.
dmancini-lmt commentedon Jan 19, 2026
Thanks for the reply @roji.
The repro already uses 10.0.2, it is imported as a transient dependency through
Microsoft.EntityFrameworkCore.Design 10.0.2, but to be sure I repeated the test with an explicit dependency onMicrosoft.EntityFrameworkCore.Relational 10.0.2and the issue presists.I did notice your comment regarding the specific scenario, but as you can see my repro is quite basic, and seems to point out that explicit usage of the same parameter multiple times triggers the problem even in 10.0.2.
roji commentedon Jan 20, 2026
Thanks, I can indeed see this happening - see minimal repro below. As this indeed probably represents a much more common scenario, I'll look at backporting dotnet/efcore#37430 to 10.0.x.
Minimal repro produced with AI