Skip to content

Commit

Permalink
Clear the parameters on DbCommand after execution.
Browse files Browse the repository at this point in the history
Fixes #81
  • Loading branch information
AndriySvyryd committed Dec 12, 2016
1 parent 4caeb60 commit 3361747
Show file tree
Hide file tree
Showing 2 changed files with 294 additions and 80 deletions.
36 changes: 34 additions & 2 deletions src/EntityFramework/Core/Objects/ObjectContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4096,12 +4096,26 @@ public virtual int ExecuteStoreCommand(TransactionalBehavior transactionalBehavi

return executionStrategy.Execute(
() => ExecuteInTransaction(
() => CreateStoreCommand(commandText, parameters).ExecuteNonQuery(),
() => ExecuteStoreCommandInternal(commandText, parameters),
executionStrategy,
startLocalTransaction: transactionalBehavior != TransactionalBehavior.DoNotEnsureTransaction,
releaseConnectionOnSuccess: true));
}

private int ExecuteStoreCommandInternal(string commandText, object[] parameters)
{
var command = CreateStoreCommand(commandText, parameters);
try
{
return command.ExecuteNonQuery();
}
finally
{
command.Parameters.Clear();
command.Dispose();
}
}

#if !NET40

/// <summary>
Expand Down Expand Up @@ -4237,7 +4251,7 @@ public Task<int> ExecuteStoreCommandAsync(TransactionalBehavior transactionalBeh
{
return await executionStrategy.ExecuteAsync(
() => ExecuteInTransactionAsync(
() => CreateStoreCommand(commandText, parameters).ExecuteNonQueryAsync(cancellationToken),
() => ExecuteStoreCommandInternalAsync(commandText, cancellationToken, parameters),
executionStrategy,
/*startLocalTransaction:*/ transactionalBehavior != TransactionalBehavior.DoNotEnsureTransaction,
/*releaseConnectionOnSuccess:*/ true, cancellationToken),
Expand All @@ -4249,6 +4263,20 @@ public Task<int> ExecuteStoreCommandAsync(TransactionalBehavior transactionalBeh
}
}

private async Task<int> ExecuteStoreCommandInternalAsync(string commandText, CancellationToken cancellationToken, object[] parameters)
{
var command = CreateStoreCommand(commandText, parameters);
try
{
return await command.ExecuteNonQueryAsync(cancellationToken);
}
finally
{
command.Parameters.Clear();
command.Dispose();
}
}

#endif

/// <summary>
Expand Down Expand Up @@ -4793,6 +4821,10 @@ public Task<ObjectResult<TElement>> ExecuteStoreQueryAsync<TElement>(string comm

if (command != null)
{
// We need to clear the parameters
// from the command in case we need to retry it
// to avoid getting the Sql parameter is contained in a collection error
command.Parameters.Clear();
command.Dispose();
}

Expand Down
Loading

0 comments on commit 3361747

Please sign in to comment.