Description
Issue 1 - Ef Core and managed identity integration
When you read the documentation of the EF Core integration of Azure Flexible PostgreSQL and managed identities you find a snipped like 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));
});
AddNpgsqlDbContext
doesn't not have a parameter named configureDataSourceBuilder
.
The other parameters that it does have do not have neither the ConnectionStringBuilder
nor UsePeriodicPasswordProvider
. Those are available on the NpgsqlDataSourceBuilder
class which is not exposed through the AddNpgsqlDbContext
Thus, I am not sure how to configure PostgreSQL with Ef Core and default azure credentials.
I believe this snipped was copy-pasted from the non-ef core postgresql setup from here
builder.AddNpgsqlDataSource(
"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));
});
This code does compile already. Whether it works or no, I don't know as I have ef-core setup.
So, is there a way I could re-use the created by the AddNpgsqlDataSource
to create the db context?
Issue 2 - how to configure azure postgres flexible server with local development environment
I have the following setup in my app host
var postgres = builder.AddAzurePostgresFlexibleServer("postgres")
.RunAsContainer(configure =>
{
configure
.WithDataBindMount("./.local_data", isReadOnly:false)
.WithPgAdmin();
});
var db = postgres.AddDatabase("MyDb");
Most of the time this is exactly what you need
- Run a postgres container when working locally
- Use deployed azure postgres flexible server when deployed
However, there are cases where you want to run the local project against the real deployed postgres server. For instance, I can't figure out how to setup the managed identity and I need to run against flexible server from debug console to iterate and understand what's going on. There are plenty of other valid reasons why you would want something like this.
Here's the issue, when I remove RunAsContainer
as soon as I start the app I see this
This issue is easy to fix, I simply need to add Azure section to the configurations.
Once I do that, instead of the app using the deployed version of the postgres database, it starts deploying a new version.
I've tried manually setting ConnectionStrings:MyDb
in configuration and point it to the real thing, but It still deploys a new instance of the server.
What am I doing wrong? Is this intended behavior?