-
Notifications
You must be signed in to change notification settings - Fork 3.4k
EF Core Cosmos generating invalid parameter names for parameterized queries in EF10 when using pattern matched object #37465
Copy link
Copy link
Milestone
Description
Bug description
When using a pattern matched object in a LINQ query like this:
switch (searchOptions)
{
case CategorySearchOptions so:
{
var itemsByPatternMatchedObjectProperty = await dbContext.TodoItems.Where(x => x.Task == so.Task).ToListAsync();
break;
}
}In EF8 and EF9, the generated parameter name is @__5__2_Task_0
In EF10, the generated parameter name is @5__2_Task
Using that parameter name in EF10 causes Cosmos to return an error.
I've included a minimal code snippet to reproduce this error aswell as console output.
We're observing this with both the Cosmos emulator and an Azure managed Cosmos database.
Thanks,
James
Your code
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
var services = new ServiceCollection();
services.AddDbContext<AppDbContext>(options =>
{
options.UseCosmos(
accountEndpoint: "**REDACTED**",
accountKey: "**REDACTED**",
databaseName: "**REDACTED**"
);
options
.LogTo(Console.WriteLine, LogLevel.Information)
.EnableDetailedErrors()
.EnableSensitiveDataLogging();
}
);
using var serviceProvider = services.BuildServiceProvider();
var dbContext = serviceProvider.GetRequiredService<AppDbContext>();
await dbContext.Database.EnsureDeletedAsync();
await dbContext.Database.EnsureCreatedAsync();
var task = "Test";
var searchOptions = new CategorySearchOptions("Test");
Console.WriteLine("_______________ GETTING BY VARIABLE _____________________");
var itemsByVariable = await dbContext.TodoItems.Where(x => x.Task == task).ToListAsync();
Console.WriteLine("_______________ GETTING BY OBJECT PROPERTY _____________________");
var itemsByObjectProperty = await dbContext.TodoItems.Where(x => x.Task == searchOptions.Task).ToListAsync();
switch (searchOptions)
{
case CategorySearchOptions so:
{
Console.WriteLine("_______________ GETTING BY PATTERN MATCHED OBJECT PROPERTY _____________________");
var itemsByPatternMatchedObjectProperty = await dbContext.TodoItems.Where(x => x.Task == so.Task).ToListAsync();
break;
}
}
public class TodoItem
{
public Guid Id { get; set; }
public string Task { get; set; }
public string Category { get; set; }
}
public record CategorySearchOptions(string Task);
public class AppDbContext : DbContext
{
public DbSet<TodoItem> TodoItems { get; set; }
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options) { }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.HasEmbeddedDiscriminatorName("Discriminator");
modelBuilder.HasDiscriminatorInJsonIds();
modelBuilder.HasDefaultContainer("Items");
modelBuilder.Entity<TodoItem>()
.HasPartitionKey(x => x.Category);
}
}Stack traces
fail: 08/01/2026 14:20:11.938 CoreEventId.QueryIterationFailed[10100] (Microsoft.EntityFrameworkCore.Query)
An exception occurred while iterating over the results of a query for context type 'AppDbContext'.
Microsoft.Azure.Cosmos.CosmosException : Response status code does not indicate success: BadRequest (400); Substatus: 0; ActivityId: ; Reason: (Response status code does not indicate success: BadRequest (400); Substatus: 0; ActivityId: ; Reason: ({"Errors":["Invalid query. Specified parameter name '@5__2_Task' is invalid. Parameter names should be in the format of symbol '@' followed by a valid identifier. E.g. @param1"]}););
---> Microsoft.Azure.Cosmos.Query.Core.Exceptions.ExpectedQueryPartitionProviderException: {"Errors":["Invalid query. Specified parameter name '@5__2_Task' is invalid. Parameter names should be in the format of symbol '@' followed by a valid identifier. E.g. @param1"]}
Verbose output
warn: 08/01/2026 14:20:03.200 CoreEventId.SensitiveDataLoggingEnabledWarning[10400] (Microsoft.EntityFrameworkCore.Infrastructure)
Sensitive data logging is enabled. Log entries and exception messages may include sensitive application data; this mode should only be enabled during development.
_______________ GETTING BY VARIABLE _____________________
info: 08/01/2026 14:20:10.325 CosmosEventId.ExecutingSqlQuery[30100] (Microsoft.EntityFrameworkCore.Database.Command)
Executing SQL query for container 'Items' in partition 'None' [Parameters=[@task='Test']]
SELECT VALUE c
FROM root c
WHERE (c["Task"] = @task)
info: 08/01/2026 14:20:11.677 CosmosEventId.ExecutedReadNext[30102] (Microsoft.EntityFrameworkCore.Database.Command)
Executed ReadNext (1246.5855 ms, 2.79 RU) ActivityId='ccbe4db7-6537-457d-9c9c-0358dc606c9e', Container='Items', Partition='None', Parameters=[@task='Test']
SELECT VALUE c
FROM root c
WHERE (c["Task"] = @task)
_______________ GETTING BY OBJECT PROPERTY _____________________
info: 08/01/2026 14:20:11.791 CosmosEventId.ExecutingSqlQuery[30100] (Microsoft.EntityFrameworkCore.Database.Command)
Executing SQL query for container 'Items' in partition 'None' [Parameters=[@searchOptions_Task='Test']]
SELECT VALUE c
FROM root c
WHERE (c["Task"] = @searchOptions_Task)
info: 08/01/2026 14:20:11.807 CosmosEventId.ExecutedReadNext[30102] (Microsoft.EntityFrameworkCore.Database.Command)
Executed ReadNext (14.3841 ms, 2.79 RU) ActivityId='b5c4b628-87e3-4e4c-a522-db301202aa83', Container='Items', Partition='None', Parameters=[@searchOptions_Task='Test']
SELECT VALUE c
FROM root c
WHERE (c["Task"] = @searchOptions_Task)
_______________ GETTING BY PATTERN MATCHED OBJECT PROPERTY _____________________
info: 08/01/2026 14:20:11.818 CosmosEventId.ExecutingSqlQuery[30100] (Microsoft.EntityFrameworkCore.Database.Command)
Executing SQL query for container 'Items' in partition 'None' [Parameters=[@5__2_Task='Test']]
SELECT VALUE c
FROM root c
WHERE (c["Task"] = @5__2_Task)
info: 08/01/2026 14:20:11.900 CosmosEventId.ExecutedReadNext[30102] (Microsoft.EntityFrameworkCore.Database.Command)
Executed ReadNext (81.3698 ms, 0 RU) ActivityId='(null)', Container='Items', Partition='None', Parameters=[@5__2_Task='Test']
SELECT VALUE c
FROM root c
WHERE (c["Task"] = @5__2_Task)
fail: 08/01/2026 14:20:11.938 CoreEventId.QueryIterationFailed[10100] (Microsoft.EntityFrameworkCore.Query)
An exception occurred while iterating over the results of a query for context type 'AppDbContext'.
Microsoft.Azure.Cosmos.CosmosException : Response status code does not indicate success: BadRequest (400); Substatus: 0; ActivityId: ; Reason: (Response status code does not indicate success: BadRequest (400); Substatus: 0; ActivityId: ; Reason: ({"Errors":["Invalid query. Specified parameter name '@5__2_Task' is invalid. Parameter names should be in the format of symbol '@' followed by a valid identifier. E.g. @param1"]}););
---> Microsoft.Azure.Cosmos.Query.Core.Exceptions.ExpectedQueryPartitionProviderException: {"Errors":["Invalid query. Specified parameter name '@5__2_Task' is invalid. Parameter names should be in the format of symbol '@' followed by a valid identifier. E.g. @param1"]}
** REST OF STACK TRACE NOT INCLUDED HERE DUE TO CHARACTER LIMIT **
--- Cosmos Diagnostics ---{"Summary":{},"name":"FeedIterator ReadNextAsync","start datetime":"2026-01-08T14:20:11.818Z","duration in milliseconds":81.3698,"data":{"Client Configuration":{"Client Created Time Utc":"2026-01-08T14:20:02.6274118Z","MachineId":"hashedMachineName:2dd52014-dfc9-bd2f-7abf-0f1cbb4961e8","NumberOfClientsCreated":1,"NumberOfActiveClients":1,"ConnectionMode":"Direct","User Agent":"cosmos-netstandard-sdk/3.51.0|1|X64|Microsoft Windows 10.0.26200|.NET 10.0.1|N| Microsoft.EntityFrameworkCore.Cosmos/10.0.1","ConnectionConfig":{"gw":"(cps:50, urto:6, p:False, httpf: False)","rntbd":"(cto: 5, icto: -1, mrpc: 30, mcpe: 65535, erd: True, pr: ReuseUnicastPort)","other":"(ed:False, be:False)"},"ConsistencyConfig":"(consistency: NotSet, prgns:[], apprgn: )","ProcessorCount":16},"Query Correlated ActivityId":"ec4df9d7-243c-4561-9171-7f2817de2527"},"children":[{"name":"Create Query Pipeline","duration in milliseconds":62.27,"children":[{"name":"Get Container Properties","duration in milliseconds":0.0196,"children":[{"name":"Get Collection Cache","duration in milliseconds":0.0033}]},{"name":"Service Interop Query Plan","duration in milliseconds":62.1798}]}]}
Process finished with exit code -532,462,766.
EF Core version
10.0.1
Database provider
Microsoft.EntityFrameworkCore.Cosmos
Target framework
.NET 10
Operating system
Windows 11
IDE
JetBrains Rider 2025.3.1
Reactions are currently unavailable
Metadata
Metadata
Assignees
Type
Fields
Give feedbackNo fields configured for Bug.