From 31339a5182866f2fc7033d4b25bffa8412e42c0c Mon Sep 17 00:00:00 2001 From: Marcus Bernander Date: Tue, 1 Jul 2025 08:03:18 +0200 Subject: [PATCH] NpgsqlRelationalConnection owns newly created connection. Resets datasource when you decide to control the ownership of a connection. Used once in the setup UseNpgsql when you pass a connection. So no practical affect aside from enforcing no datasource. The owned flag does not impact NpgsqlRelationalConnection CloneWith either, which seems like a bug to me in the RelationalConnection. Since the connection is opened in RelationalConnection it takes it as it should close it when Close is called. The dispose ignores it. Would make more sense to also enforce the ownership in Close and not only in Dispose, but that is not related to this repo. This would however make it more correct since the datasource is reset and the RelationalConnection knows it has ownership of the connection when the connection is directly injected from a newly created connection which is not tracked elsewhere. --- .../Infrastructure/Internal/NpgsqlOptionsExtension.cs | 10 ++++++++++ .../Storage/Internal/NpgsqlRelationalConnection.cs | 4 ++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/EFCore.PG/Infrastructure/Internal/NpgsqlOptionsExtension.cs b/src/EFCore.PG/Infrastructure/Internal/NpgsqlOptionsExtension.cs index 928e75438..ead71b81e 100644 --- a/src/EFCore.PG/Infrastructure/Internal/NpgsqlOptionsExtension.cs +++ b/src/EFCore.PG/Infrastructure/Internal/NpgsqlOptionsExtension.cs @@ -182,6 +182,16 @@ public override RelationalOptionsExtension WithConnection(DbConnection? connecti return clone; } + /// + public override RelationalOptionsExtension WithConnection(DbConnection? connection, bool owned) + { + var clone = (NpgsqlOptionsExtension)base.WithConnection(connection, owned); + + clone.DataSource = null; + + return clone; + } + /// /// Returns a copy of the current instance configured with the specified range mapping. /// diff --git a/src/EFCore.PG/Storage/Internal/NpgsqlRelationalConnection.cs b/src/EFCore.PG/Storage/Internal/NpgsqlRelationalConnection.cs index 89df883b0..2f4bf1ca6 100644 --- a/src/EFCore.PG/Storage/Internal/NpgsqlRelationalConnection.cs +++ b/src/EFCore.PG/Storage/Internal/NpgsqlRelationalConnection.cs @@ -205,7 +205,7 @@ public virtual INpgsqlRelationalConnection CreateAdminConnection() var adminNpgsqlOptions = DataSource is not null ? npgsqlOptions.WithConnection(((NpgsqlConnection)CreateDbConnection()).CloneWith(adminConnectionString)) : npgsqlOptions.Connection is not null - ? npgsqlOptions.WithConnection(DbConnection.CloneWith(adminConnectionString)) + ? npgsqlOptions.WithConnection(DbConnection.CloneWith(adminConnectionString), owned: true) : npgsqlOptions.WithConnectionString(adminConnectionString); var optionsBuilder = new DbContextOptionsBuilder(); @@ -243,7 +243,7 @@ public virtual async ValueTask CloneWith( var relationalOptions = RelationalOptionsExtension.Extract(Dependencies.ContextOptions) .WithConnectionString(null) - .WithConnection(clonedDbConnection); + .WithConnection(clonedDbConnection, owned: true); var optionsBuilder = new DbContextOptionsBuilder(); ((IDbContextOptionsBuilderInfrastructure)optionsBuilder).AddOrUpdateExtension(relationalOptions);