I'm trying to use EntityFramework6.Npgsqlcom transaction, however I get an exception when I call the DbContext.UseTransaction method and step a transaction.
Error message (Last InnerException):
"Npgsql.PostgresException (0x80004005): 25P02: current transaction is aborted, commands ignored until transaction end \ 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" Home package id="EntityFramework6.Npgsql" version="3.1.1" targetFramework="net462" Home package id="Npgsql" version="3.1.2" targetFramework="net462"
The strangest thing is that in the second method call, everything happens normally. After a few hours investigating the problem (using other databases it works normally), I found that by changing the version of EntityFramework used, the project works again.
Packages that work
package id="EntityFramework" version="6.1.0" targetFramework="net462" Home package id="EntityFramework6.Npgsql" version="3.1.1" targetFramework="net462" Home package id="Npgsql" version="3.1.2" targetFramework="net462"
Has anyone else had this problem ?? Is there a bug in Npgsql / EntityFramework6.Npgsql that does not support the latest version of EntityFramework (6.1.3)?