Transaction Problem with generated Stored Procedure Call

Discussion of open issues, suggestions and bugs regarding Entity Developer - ORM modeling and code generation tool
Post Reply
HelmutJ
Posts: 3
Joined: Mon 13 Nov 2017 14:25

Transaction Problem with generated Stored Procedure Call

Post by HelmutJ » Mon 04 Jun 2018 13:36

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

Shalex
Site Admin
Posts: 8125
Joined: Thu 14 Aug 2008 12:44

Re: Transaction Problem with generated Stored Procedure Call

Post by Shalex » Thu 07 Jun 2018 18:26

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: 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.

Post Reply