From 6eee71494dd4953139476399514113fb882dfc7e Mon Sep 17 00:00:00 2001 From: Laurents Meyer Date: Sun, 3 Dec 2023 16:22:44 +0100 Subject: [PATCH] Add failing test that exposes DbDataSource related bug. --- .../NpgsqlRelationalConnectionTest.cs | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/test/EFCore.PG.Tests/NpgsqlRelationalConnectionTest.cs b/test/EFCore.PG.Tests/NpgsqlRelationalConnectionTest.cs index 8d152daed..cc465a4f4 100644 --- a/test/EFCore.PG.Tests/NpgsqlRelationalConnectionTest.cs +++ b/test/EFCore.PG.Tests/NpgsqlRelationalConnectionTest.cs @@ -81,6 +81,46 @@ public void Uses_DbDataSource_from_application_service_provider() Assert.Equal("Host=FakeHost", connection2.ConnectionString); } + [Fact] + public void Uses_correct_DbDataSource_from_application_service_provider_with_cached_DbContextOptions_extension() + { + var serviceCollection1 = new ServiceCollection(); + + serviceCollection1 + .AddNpgsqlDataSource("Host=FakeHost1") + .AddDbContext(o => o.UseNpgsql()); + + using (var serviceProvider1 = serviceCollection1.BuildServiceProvider()) + { + var dataSource1 = serviceProvider1.GetRequiredService(); + + Assert.Equal("Host=FakeHost1", dataSource1.ConnectionString); + + var context1 = serviceProvider1.GetRequiredService(); + var relationalConnection1 = (NpgsqlRelationalConnection)context1.GetService()!; + + Assert.Same(dataSource1, relationalConnection1.DbDataSource); + } + + var serviceCollection2 = new ServiceCollection(); + + serviceCollection2 + .AddNpgsqlDataSource("Host=FakeHost2") + .AddDbContext(o => o.UseNpgsql()); + + using (var serviceProvider2 = serviceCollection2.BuildServiceProvider()) + { + var dataSource2 = serviceProvider2.GetRequiredService(); + + Assert.Equal("Host=FakeHost2", dataSource2.ConnectionString); + + var context2 = serviceProvider2.GetRequiredService(); + var relationalConnection2 = (NpgsqlRelationalConnection)context2.GetService()!; + + Assert.Same(dataSource2, relationalConnection2.DbDataSource); + } + } + [Fact] public void Can_create_master_connection_with_connection_string() {