Skip to content

Commit 849463d

Browse files
committed
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.
1 parent fe4269f commit 849463d

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

src/EFCore.PG/Infrastructure/Internal/NpgsqlOptionsExtension.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,16 @@ public override RelationalOptionsExtension WithConnection(DbConnection? connecti
182182
return clone;
183183
}
184184

185+
/// <inheritdoc />
186+
public override RelationalOptionsExtension WithConnection(DbConnection? connection, bool owned)
187+
{
188+
var clone = (NpgsqlOptionsExtension)base.WithConnection(connection, owned);
189+
190+
clone.DataSource = null;
191+
192+
return clone;
193+
}
194+
185195
/// <summary>
186196
/// Returns a copy of the current instance configured with the specified range mapping.
187197
/// </summary>

src/EFCore.PG/Storage/Internal/NpgsqlRelationalConnection.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ public virtual INpgsqlRelationalConnection CreateAdminConnection()
205205
var adminNpgsqlOptions = DataSource is not null
206206
? npgsqlOptions.WithConnection(((NpgsqlConnection)CreateDbConnection()).CloneWith(adminConnectionString))
207207
: npgsqlOptions.Connection is not null
208-
? npgsqlOptions.WithConnection(DbConnection.CloneWith(adminConnectionString))
208+
? npgsqlOptions.WithConnection(DbConnection.CloneWith(adminConnectionString, owned: true))
209209
: npgsqlOptions.WithConnectionString(adminConnectionString);
210210

211211
var optionsBuilder = new DbContextOptionsBuilder();
@@ -243,7 +243,7 @@ public virtual async ValueTask<INpgsqlRelationalConnection> CloneWith(
243243

244244
var relationalOptions = RelationalOptionsExtension.Extract(Dependencies.ContextOptions)
245245
.WithConnectionString(null)
246-
.WithConnection(clonedDbConnection);
246+
.WithConnection(clonedDbConnection, owned: true);
247247

248248
var optionsBuilder = new DbContextOptionsBuilder();
249249
((IDbContextOptionsBuilderInfrastructure)optionsBuilder).AddOrUpdateExtension(relationalOptions);

0 commit comments

Comments
 (0)