Wrong document for adding Azure authenticated NpgsqlDbContext
As of Aspire 9.0, Azure Database for PostgreSQL resources were updated to use Microsoft Entra ID by default.
Then, I start integration according to this article.
It says
builder.AddNpgsqlDbContext<YourDbContext>(
"postgresdb",
configureDataSourceBuilder: (dataSourceBuilder) =>
{
if (!string.IsNullOrEmpty(dataSourceBuilder.ConnectionStringBuilder.Password))
{
return;
}
dataSourceBuilder.UsePeriodicPasswordProvider(async (_, ct) =>
{
var credentials = new DefaultAzureCredential();
var token = await credentials.GetTokenAsync(
new TokenRequestContext([
"https://ossrdbms-aad.database.windows.net/.default"
]), ct);
return token.Token;
},
TimeSpan.FromHours(24),
TimeSpan.FromSeconds(10));
});
but no such overload for AddNpgsqlDbContext exists.
same issue here
Hey @davidfowl 👋 I see you've transferred this to docs from the Aspire repo, but I think this is actually an Aspire issue.
The overload referred to was added to src/Components/Aspire.Npgsql/AspirePostgreSqlNpgsqlExtensions.cs (see: https://github.com/dotnet/aspire/pull/1481), however it is included in the Aspire.NpqSql nuget package v 8.2.2, but not the Aspire.NpqSql.EntityFrameworkCore.PostgreSQL package v 8.2.2, and in neither for v 9.0.0.
I'm not sure why, the overload is still there on the main branch: https://github.com/dotnet/aspire/blob/ad430a1415c44c7de17651f09a10cc23057c3386/src/Components/Aspire.Npgsql/AspirePostgreSqlNpgsqlExtensions.cs#L33
Although there seems to only be an extension for AddNpgsqlDataSource, and not for AddNpgsqlDbContext (so no EF Core support for periodic password provider).
Apologies if I've misunderstood, this is just from me poking around, I don't 100% understand what I'm looking at here. But it looks like a problem on the Apsire side, not the docs side.
cc: @aaronpowell
Hey @davidfowl 👋 I see you've transferred this to docs from the Aspire repo, but I think this is actually an Aspire issue.
The overload referred to was added to
src/Components/Aspire.Npgsql/AspirePostgreSqlNpgsqlExtensions.cs(see: dotnet/aspire#1481), however it is included in theAspire.NpqSqlnuget package v8.2.2, but not theAspire.NpqSql.EntityFrameworkCore.PostgreSQLpackage v8.2.2, and in neither for v9.0.0.I'm not sure why, the overload is still there on the main branch: https://github.com/dotnet/aspire/blob/ad430a1415c44c7de17651f09a10cc23057c3386/src/Components/Aspire.Npgsql/AspirePostgreSqlNpgsqlExtensions.cs#L33
Although there seems to only be an extension for
AddNpgsqlDataSource, and not forAddNpgsqlDbContext(so no EF Core support for periodic password provider).Apologies if I've misunderstood, this is just from me poking around, I don't 100% understand what I'm looking at here. But it looks like a problem on the Apsire side, not the docs side.
cc: @aaronpowell
Apologies, I was completely wrong. It is in fact still there in 9.0.0, and the docs do in fact only specify AddNpgsqlDataSource for the periodic password provider.
So definitely not a docs issue, and I guess adding it for EF Core is a feature request?
@RamType0 @jjkopmels this is working for me:
builder.AddNpgsqlDataSource(
ServiceConstants.APP_DATABASE, // I've got all my service names set as constants in a shared project
configureDataSourceBuilder: (dataSourceBuilder) =>
{
if (!string.IsNullOrEmpty(dataSourceBuilder.ConnectionStringBuilder.Password))
{
return;
}
dataSourceBuilder.UsePeriodicPasswordProvider(async (_, ct) =>
{
var credentials = new DefaultAzureCredential();
var token = await credentials.GetTokenAsync(
new TokenRequestContext([
"https://ossrdbms-aad.database.windows.net/.default"
]), ct);
return token.Token;
},
TimeSpan.FromHours(24),
TimeSpan.FromSeconds(10));
});
builder.Services.AddDbContext<ApplicationDbContext>((serviceProvider, options) =>
{
var dataSource = serviceProvider.GetRequiredService<NpgsqlDataSource>();
options.UseNpgsql(dataSource);
});
Closing as the docs have migrated to https://github.com/microsoft/aspire.dev. The incorrect AddNpgsqlDbContext API sample may still exist in the new docs — please file on microsoft/aspire.dev if still relevant.