Skip to content

Unable to use (Sql)Parameter with FromSqlRaw with Cosmos DB #37252

@GregoireWulliamoz

Description

@GregoireWulliamoz

Bug description

I am using Entity Framework Core with Cosmos DB and want to perform parameterized queries using FromSqlRaw, in the same manner explained in the doc: https://learn.microsoft.com/en-us/ef/core/querying/sql-queries?tabs=sqlserver#dynamic-sql-and-parameters
The issue is that the name of parameters are not taken into account, and replaced by @p0, @p1, @p2.
It's even more annoying when pagination is applied on top of FromSqlRaw, because there is a behavior change between version 9 and 10. In version 9. Limit and offset parameters where named @__p_2 and @__p_3 in version 9, and changed to @p0 and @p1 in version 10 and the previous parameters are just discarded

Your code

var chainParameter = new SqlParameter("@chain", "SomeChainValue");
var levelParameter = new SqlParameter("@Level", "SomeLevelValue");

var result = await CosmosQueryableExtensions.FromSqlRaw(
        dbContext.Promotions,
        $"""
         SELECT *
         FROM c
         WHERE
         c.Chain = {chainParameter.ParameterName} AND
         c.Level = {levelParameter.ParameterName}
         """,
        chainParameter, levelParameter)
    .AsNoTracking()
    .Skip(0)
    .Take(50)
    .ToListAsync(cancellationToken);

Stack traces

Here you can find the generated queries

// Entity framework 9.0.11
Executing SQL query for container 'XXX' in partition 'None' [Parameters=[@p0='@chain', @p1='@Level', @__p_1='0', @__p_2='50']]
SELECT VALUE s
FROM (
    SELECT *
    FROM c
    WHERE
    c.Chain = @chain AND
    c.Level = @Level
) s
OFFSET @__p_1 LIMIT @__p_2

// Entity framework 10.0.0
Executing SQL query for container 'XXX' in partition 'None' [Parameters=[@p0='@chain', @p1='@Level']]
SELECT VALUE s
FROM (
    SELECT *
    FROM c
    WHERE
    c.Chain = @chain AND
    c.Level = @Level
) s
OFFSET @p0 LIMIT @p1

Verbose output


EF Core version

10.0.0

Database provider

Microsoft.EntityFrameworkCore.Cosmos

Target framework

.NET 10

Operating system

Windows 11

IDE

Visual Studio 2016

Metadata

Metadata

Assignees

Type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions