Skip to content

Future query fails with exception "The variable name has already been declared" #647

@bedpans

Description

@bedpans

The following query results in an exception below:

var query1 = dbContext.Products.Where(x => storeIds.Contains(x.StoreId)).Select(x => x.ProductId);
var query2 = dbContext.Employees.Where(x => storeIds.Contains(x.StoreId)).Select(x => x.EmployeeId);
var query3 = query1.Union(query2);

dbContext.Products.AsQueryable().Future();
await query3.Future().ToListAsync();
Unhandled exception. Microsoft.Data.SqlClient.SqlException (0x80131904): The variable name '@Z_2___storeIds_0' has already been declared. Variable names must be unique within a query batch or stored procedure.
Must declare the scalar variable "@Z_2___storeIds_0_1".
Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon.

This issue occurs on versions 8.x, and 9.x, but not 3.x, 7x., and 10.x. I am only able to test this on SQL Server, but it might not reproduce on other databases. Here's sample project which reproduces the issue, although you will need an SQL Server database to run it.
ConsoleApp1.zip

I have also created an issue for EF Core as I believe it should not generate query with duplicate parameters, however EF Core's query is valid and executes successfully. Exception occurs only when such query is executed as future query.

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions