Skip to content

Commit f3e21dc

Browse files
authored
Provide a better exception message for invalid data source config. (#2796)
1 parent 5784b52 commit f3e21dc

1 file changed

Lines changed: 19 additions & 5 deletions

File tree

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

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Globalization;
33
using System.Net.Security;
44
using System.Text;
5+
using Npgsql.EntityFrameworkCore.PostgreSQL.Internal;
56

67
namespace Npgsql.EntityFrameworkCore.PostgreSQL.Infrastructure.Internal;
78

@@ -242,12 +243,25 @@ public override void Validate(IDbContextOptions options)
242243
var dataSource = DataSource
243244
?? options.FindExtension<CoreOptionsExtension>()?.ApplicationServiceProvider?.GetService<NpgsqlDataSource>();
244245

245-
if (dataSource is not null
246-
&& (ProvideClientCertificatesCallback is not null
247-
|| RemoteCertificateValidationCallback is not null
248-
|| ProvidePasswordCallback is not null))
246+
if (dataSource is not null)
249247
{
250-
throw new InvalidOperationException();
248+
if (ProvideClientCertificatesCallback is not null)
249+
{
250+
throw new InvalidOperationException(
251+
"When passing an NpgsqlDataSource to UseNpgsql(), call 'ProvideClientCertificatesCallback' on NpgsqlDataSourceBuilder rather than in UseNpgsql().");
252+
}
253+
254+
if (RemoteCertificateValidationCallback is not null)
255+
{
256+
throw new InvalidOperationException(
257+
"When passing an NpgsqlDataSource to UseNpgsql(), call 'RemoteCertificateValidationCallback' on NpgsqlDataSourceBuilder rather than in UseNpgsql().");
258+
}
259+
260+
if (ProvidePasswordCallback is not null)
261+
{
262+
throw new InvalidOperationException(
263+
"When passing an NpgsqlDataSource to UseNpgsql(), 'ProviderPasswordCallback' cannot be used in UseNpgsql(). See https://www.npgsql.org/doc/security.html for configuring passwords and token rotation on NpgsqlDataSourceBuilder.");
264+
}
251265
}
252266

253267
if (UseRedshift && _postgresVersion is not null)

0 commit comments

Comments
 (0)