dotnet / dotnet/efcore

Parameter value mixup

Open
#32,932 4 comments 7 reactions 0 assignees View on GitHub
area-query customer-reported
Dominant language
C#
Stars
14.8k
Forks
3.4k
PR merge metrics
PR metrics pending

Description

I wrote two manual SQL queries with parameters with the same names but with different values like this:

```
var result = await Enumerable
.Range(1, 2)
.Select(index => dbContext
.Set()
.FromSqlRaw(
"SELECT @date AS Date",
new SqlParameter("date", new DateOnly(2024, 1, index))))
.Aggregate(Queryable.Union)
.ToListAsync();

foreach (var line in result)
{
Console.WriteLine(line.Date);
}
```

I expected to get two results with 2024-01-01 and 2024-01-02 but got only the result 2024-01-01.
However, this works as expected:

```
var result = await Enumerable
.Range(1, 2)
.Select(index => dbContext
.Set()
.FromSqlRaw(
"SELECT {0} AS Date",
new DateOnly(2024, 1, index)))
.Aggregate(Queryable.Union)
.ToListAsync();

foreach (var line in result)
{
Console.WriteLine(line.Date);
}
```

I have created an example project showcasing the bug: https://github.com/samuel-utbult-oborgen/ef-core-bug
I suspect this could be because the variable "date" is created for the combined query and its value is set to 2024-01-01 while the value 2024-01-02 is ignored. The parameters should reasonably be isolated between the queries as they are specified for each query respectively.

EF Core version: 8.0.1
Database provider: (e.g. Microsoft.EntityFrameworkCore.SqlServer)
Target framework: (e.g. .NET 8.0)
Operating system: macOS Sonoma 14.2.1

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.