-
Notifications
You must be signed in to change notification settings - Fork 257
Description
I just updated our codebase from ASP .NET6 to .NET8.
In the process of doing so, I was now also able to update the Npgsql.EntityFrameworkCore.PostgreSQL package from 7.0.11 to 8.0.4.
I was since able to recreate the Migration with the new packages without problems.
However, since then was getting the following Error when debugging or running Unit Tests:
System.InvalidOperationException : Connection already open
TearDown : System.NullReferenceException : Object reference not set to an instance of an object.
StackTrace: at Npgsql.ThrowHelper.ThrowInvalidOperationException(String message)
at Npgsql.NpgsqlConnection.CheckClosed()
at Npgsql.NpgsqlConnection.Open(Boolean async, CancellationToken cancellationToken)
at Npgsql.NpgsqlConnection.Open()
at Npgsql.EntityFrameworkCore.PostgreSQL.Migrations.Internal.NpgsqlMigrator.Migrate(String targetMigration)
at Microsoft.EntityFrameworkCore.RelationalDatabaseFacadeExtensions.Migrate(DatabaseFacade databaseFacade)
at Contexts.EntityDbContext.Init()
It seems the casue for the Issue is that I am manually opening a Transaction before migrating the Database.
This will in effect open the Connection, and afterwards Ef-postgres also tries to always open the Connection again.
Problematic Code:
using var initMigration = context.BeginTransaction();
context.Database.Migrate(); // <--- casues "Connection already open"
initMigration.Commit();
This was however working in the previously installed Version (7.0.11) and so it's a regression/bug imo.
Or was this an intended change?
For now the fix was to simply comment out the transaction-code:
// using var initMigration = context.BeginTransaction();
context.Database.Migrate(); // Ok
// initMigration.Commit();
But I think the best solution would be to check whether the Connection is already Open, before trying to open it again.
Thanks in advance!