Skip to content

Commit

Permalink
Query: Clear DbCommand parameters so that FromSql parameters can be u…
Browse files Browse the repository at this point in the history
…sed multiple times
  • Loading branch information
smitpatel committed Aug 19, 2016
1 parent 768968f commit bbd1745
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -599,17 +599,39 @@ public virtual void From_sql_with_dbParameter_mixed()
[Fact]
public virtual void Include_does_not_close_user_opened_connection_for_empty_result()
{
using (var ctx = CreateContext())
using (var context = CreateContext())
{
ctx.Database.OpenConnection();
context.Database.OpenConnection();

var query = ctx.Customers
var query = context.Customers
.Include(v => v.Orders)
.Where(v => v.CustomerID == "MAMRFC")
.ToList();

Assert.Empty(query);
Assert.Equal(ConnectionState.Open, ctx.Database.GetDbConnection().State);
Assert.Equal(ConnectionState.Open, context.Database.GetDbConnection().State);
}
}

[Fact]
public virtual void From_sql_with_db_parameters_called_multiple_times()
{
using (var context = CreateContext())
{
var parameter = CreateDbParameter("@id", "ALFKI");

var query = context.Customers
.FromSql(@"SELECT * FROM ""Customers"" WHERE ""CustomerID"" = @id",
parameter);

var result1 = query.ToList();

Assert.Equal(1 , result1.Count);

// This should not throw exception.
var result2 = query.ToList();

Assert.Equal(1, result2.Count);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ internal static readonly MethodInfo FromSqlMethodInfo
/// <para>
/// You can also construct a DbParameter and supply it to as a parameter value. This allows you to use named
/// parameters in the SQL query string -
/// <code>context.Blogs.SqlQuery("SELECT * FROM [dbo].[SearchBlogs]({@searchTerm})", new SqlParameter("@searchTerm", userSuppliedSearchTerm))</code>
/// <code>context.Blogs.FromSql("SELECT * FROM [dbo].[SearchBlogs]({@searchTerm})", new SqlParameter("@searchTerm", userSuppliedSearchTerm))</code>
/// </para>
/// </summary>
/// <typeparam name="TEntity"> The type of the elements of <paramref name="source" />. </typeparam>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,8 @@ protected virtual object Execute(
}
finally
{
dbCommand.Parameters.Clear();

if (closeConnection)
{
connection.Close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public override void AddRange(Array values)

public override void Clear()
{
throw new NotImplementedException();
// no-op to test that parameters are passed correctly to db command.
}

public override bool Contains(string value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,21 @@ public override void From_sql_with_dbParameter_mixed()
Sql);
}

public override void From_sql_with_db_parameters_called_multiple_times()
{
base.From_sql_with_db_parameters_called_multiple_times();

Assert.Equal(
@"@id: ALFKI (Nullable = false) (Size = 5)
SELECT * FROM ""Customers"" WHERE ""CustomerID"" = @id
@id: ALFKI (Nullable = false) (Size = 5)
SELECT * FROM ""Customers"" WHERE ""CustomerID"" = @id",
Sql);
}

public FromSqlQuerySqlServerTest(NorthwindQuerySqlServerFixture fixture, ITestOutputHelper testOutputHelper)
: base(fixture)
{
Expand Down

0 comments on commit bbd1745

Please sign in to comment.