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);