Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FromSql query parameter name collisions #2309

Closed
mikary opened this issue Jun 1, 2015 · 0 comments
Closed

FromSql query parameter name collisions #2309

mikary opened this issue Jun 1, 2015 · 0 comments
Assignees
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Milestone

Comments

@mikary
Copy link
Contributor

mikary commented Jun 1, 2015

When queries with multiple query sources having calls to FromSql with parameters generate SQL, the query parameter names are not unique and an exception is thrown.

[Fact]
public virtual void From_sql_queryable_multiple_composed_with_parameters_and_closure_parameters()
{
    var city = "London";
    var startDate = new DateTime(1997, 1, 1);
    var endDate = new DateTime(1998, 1, 1);

    using (var context = CreateContext())
    {
        var actual
            = (from c in context.Set<Customer>().FromSql("SELECT * FROM Customers WHERE City = {0}", city)
                from o in context.Set<Order>().FromSql("SELECT * FROM Orders WHERE OrderDate BETWEEN {0} AND {1}",
                startDate,
                endDate)
                where c.CustomerID == o.CustomerID
                select new { c, o })
                .ToArray();
    }
}
Test 'Microsoft.Data.Entity.SqlServer.FunctionalTests.FromSqlQuerySqlServerTest.From_sql_queryable_multiple_composed_with_parameters_and_closure_parameters' failed:
    System.Data.SqlClient.SqlException : The variable name '@p0' has already been declared. Variable names must be unique within a query batch or stored procedure.
Must declare the scalar variable "@p1".
    at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
    at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
    at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
    at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
    at System.Data.SqlClient.SqlDataReader.TryConsumeMetaData()
    at System.Data.SqlClient.SqlDataReader.get_MetaData()
    at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
    at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds, Boolean describeParameterEncryptionRequest)
    at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite)
    at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
    at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)
    at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
    at System.Data.Common.DbCommand.ExecuteReader()
    Query\QueryingEnumerable.cs(79,0): at Microsoft.Data.Entity.Relational.Query.QueryingEnumerable.Enumerator.MoveNext()
    at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
    at System.Linq.Enumerable.<SelectManyIterator>d__1`2.MoveNext()
    at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
    Query\LinqOperatorProvider.cs(144,0): at Microsoft.Data.Entity.Query.LinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExceptionInterceptor.MoveNext()
    at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
    at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
    FromSqlQueryTestBase.cs(133,0): at Microsoft.Data.Entity.Relational.FunctionalTests.FromSqlQueryTestBase`1.From_sql_queryable_multiple_composed_with_parameters_and_closure_parameters()
    FromSqlQuerySqlServerTest.cs(13,0): at Microsoft.Data.Entity.SqlServer.FunctionalTests.FromSqlQuerySqlServerTest.From_sql_queryable_multiple_composed_with_parameters_and_closure_parameters()
@rowanmiller rowanmiller added this to the 7.0.0 milestone Jun 2, 2015
@mikary mikary added the 2 - Done label Jun 5, 2015
@mikary mikary closed this as completed Jun 17, 2015
@mikary mikary modified the milestones: 7.0.0-beta6, 7.0.0 Jun 17, 2015
@ajcvickers ajcvickers added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Oct 15, 2022
@ajcvickers ajcvickers modified the milestones: 1.0.0-beta6, 1.0.0 Oct 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Projects
None yet
Development

No branches or pull requests

3 participants