Page 1 of 1

Transaction Problem with generated Stored Procedure Call

Posted: Mon 04 Jun 2018 13:36
by HelmutJ

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:

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

Re: Transaction Problem with generated Stored Procedure Call

Posted: Thu 07 Jun 2018 18:26
by Shalex
1. There is a line of code generated by a current EF Core template:

Code: Select all

            DbConnection connection = this.Database.GetDbConnection();
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: ... .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.