Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AddNpgsqlDbContext is missing configureDataSourceBuilder parameter #7746

Closed
1 task done
julioct opened this issue Feb 23, 2025 · 1 comment
Closed
1 task done

AddNpgsqlDbContext is missing configureDataSourceBuilder parameter #7746

julioct opened this issue Feb 23, 2025 · 1 comment
Labels
area-integrations Issues pertaining to Aspire Integrations packages

Comments

@julioct
Copy link
Contributor

julioct commented Feb 23, 2025

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

The docs say that you should be able to do this, which is ideal for using PostgreSQL (EF Core) with managed identities:

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 the configureDataSourceBuilder parameter is just not there.

Where is that parameter and how do we use managed identities with PostgreSQL when using EF Core?

Expected Behavior

You should be able to do this:

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

Steps To Reproduce

  1. Add a reference to Aspire.Npgsql.EntityFrameworkCore.PostgreSQL
  2. Try to use AddNpgsqlDbContext with the configureDataSourceBuilder parameter.
  3. The parameter is not there, won't compile.

Exceptions (if any)

No response

.NET Version info

.NET SDK:
Version: 8.0.406
Commit: 73c77e847d
Workload version: 8.0.400-manifests.f7661779
MSBuild version: 17.11.22+17752cebd

Runtime Environment:
OS Name: Windows
OS Version: 10.0.26100
OS Platform: Windows
RID: win-x64
Base Path: C:\Program Files\dotnet\sdk\8.0.406\

.NET workloads installed:
Configured to use loose manifests when installing new manifests.
There are no installed workloads to display.

Host:
Version: 8.0.13
Architecture: x64
Commit: eba546b0f0

.NET SDKs installed:
8.0.406 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
Microsoft.AspNetCore.App 8.0.13 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 8.0.13 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 8.0.13 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Other architectures found:
None

Environment variables:
Not set

global.json file:
Not found

Anything else?

.NET Aspire Version: 9.0
Aspire.Npgsql.EntityFrameworkCore.PostgreSQL version: 9.0.0
IDE: VS Code

@github-actions github-actions bot added the area-integrations Issues pertaining to Aspire Integrations packages label Mar 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-integrations Issues pertaining to Aspire Integrations packages
Projects
None yet
Development

No branches or pull requests

4 participants