Transaction with Entitiframework 6.1.3 and Postgressql does not work correctly

Asked

Viewed 49 times

1

I’m trying to use Entityframework6.Npgsqlcom transaction, however I get an Exception when I call the Dbcontext.Usetransaction method and pass a transaction.

Error message (Last Innerexception):

"Npgsql.Postgresexception (0x80004005): 25P02: Current transaction is aborted, Commands Ignored until end of transaction block r n in Npgsql.Npgsqlconnector.

So I created a project to simulate and try to find the real problem:

Test Project

[Route("")]
    public async Task<IHttpActionResult> Get()
    {
        try
        {
            using (var conn = new Npgsql.NpgsqlConnection(ConfigurationManager.AppSettings["app:pgconn"]))
            {
                conn.Open();
                using (var transaction = conn.BeginTransaction(System.Data.IsolationLevel.ReadUncommitted))
                {
                    using (var ctx = new CadastrosContext(conn))
                    {
                        var tenant = new Tenant();



                        ctx.Database.UseTransaction(transaction);

                        ctx.Tenants.Add(tenant);
                        await ctx.SaveChangesAsync()
                            .ConfigureAwait(false);

                        Debug.WriteLine($"Tenant 1 Id: {tenant.TenantId}");
                    }

                    using (var ctx = new CadastrosContext(conn))
                    {
                        var tenant = new Tenant();



                        ctx.Database.UseTransaction(transaction);

                        ctx.Tenants.Add(tenant);
                        await ctx.SaveChangesAsync()
                            .ConfigureAwait(false);

                        Debug.WriteLine($"Tenant 2 Id: {tenant.TenantId}");
                    }

                    await transaction.CommitAsync();
                }
            }
        }
        catch (Exception ex)
        {
            throw;
        }


        return Ok();
    }

Packages used

package id="Entityframework" version="6.1.3" targetFramework="net462"
package id="Entityframework6.Npgsql" version="3.1.1" targetFramework="net462"
package id="Npgsql" version="3.1.2" targetFramework="net462"

The strange thing is that in the second call of the method, everything happens normally. After a few hours investigating the problem (using other databases works normally), I found that by changing the version used of Entityframework), the project works normally again.

Working packages

package id="Entityframework" version="6.1.0" targetFramework="net462"
package id="Entityframework6.Npgsql" version="3.1.1" targetFramework="net462"
package id="Npgsql" version="3.1.2" targetFramework="net462"

Did anyone else have this problem? Is there a bug in Npgsql/Entityframework6.Npgsql that does not support the latest version of Entityframework (6.1.3)?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.