verdie-g / StoredProcedureEFCore

Entity Framework Core extension to execute stored procedures
MIT License
192 stars 44 forks source link

Usage with transactions #25

Closed johanvergeer closed 5 years ago

johanvergeer commented 5 years ago

Hello,

So far I really like your library, but now I ran into an issue when using a transaction.

I have this method that call a couple of stored procedures that should all roll back when an error occurs.

protected async Task SaveOrder(OrderEntity order)
{
    using (var transaction = await this.Context.Database.BeginTransactionAsync())
    {
        await this.Context.LoadStoredProc("[dbo].[InsertOrder]")
                .AddParam("@OrderId", order.Id)
                .AddParam("@OrderDate", order.Date)
                .ExecNonQueryAsync();

        foreach(var line in order.Lines)
        {
            await this.Context.LoadStoredProc("[dbo].[InsertOrderLine]")
                .AddParam("@OrderId", order.Id)
                .AddParam("@LineNr", line.Id)
                .AddParam("@Quantity", line.Quantity)
                .ExecNonQueryAsync();
        }

        transaction.Commit();
    }
}

But when I run this it throws an exception:

System.InvalidOperationException : BeginExecuteNonQuery requires the command to have a transaction when the connection assigned to the command is in a pending local transaction.  The Transaction property of the command has not been initialized.

So now I'm looking for a way to set the transaction on the Command. Is it possible to do this?

verdie-g commented 5 years ago

Fixed in 0.3.14.

johanvergeer commented 5 years ago

Thanks @verdie-g I'll upgrade to the next version and give it a try.