Hi,
I tried to make a stored proc method call inside a transaction. This ran into a runtime error. So I had to do two small changes in the ef core template. Perhaps you can add them as well to the standard template:
Line 1028 add: using Microsoft.EntityFrameworkCore.Storage;
Line 1909 add: cmd.Transaction = Database.CurrentTransaction?.GetDbTransaction();
For your understanding of the correct place:
try
{
using (DbCommand cmd = connection.CreateCommand())
{
if (this.Database.GetCommandTimeout().HasValue)
cmd.CommandTimeout = this.Database.GetCommandTimeout().Value;
cmd.Transaction = Database.CurrentTransaction?.GetDbTransaction();
<#+
if (!string.IsNullOrEmpty(method.Procedure) && !method.TableValuedFunction) {
#>
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = @"<#= codeProvider.GetQuotedString(method.Procedure) #>";
<#+
} else {
#>
Best Regards
Helmut Joost
Transaction Problem with generated Stored Procedure Call
Re: Transaction Problem with generated Stored Procedure Call
1. There is a line of code generated by a current EF Core template:
Usually, a transaction associated with the connection object is promoted to the command object by connection.CreateCommand().
2. You may create an ambient transaction and put method call into it to make sure that TransactionScope is used: https://msdn.microsoft.com/en-us/librar ... .110).aspx.
3. The predefined EF Core template is common for all supported providers. Changing a current behavior may break a backward compatibility for some providers.
Code: Select all
DbConnection connection = this.Database.GetDbConnection();
2. You may create an ambient transaction and put method call into it to make sure that TransactionScope is used: https://msdn.microsoft.com/en-us/librar ... .110).aspx.
3. The predefined EF Core template is common for all supported providers. Changing a current behavior may break a backward compatibility for some providers.