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

Having multiple AddNpgsqlDbContext calls results in second and subsequent contexts using the wrong connection string. #2096

Closed
mitchdenny opened this issue Feb 6, 2024 · 6 comments · Fixed by #2097
Assignees
Labels
area-integrations Issues pertaining to Aspire Integrations packages

Comments

@mitchdenny
Copy link
Member

In the following playground we initialize multiple Postgres databases for use with EF:

https://github.com/dotnet/aspire/tree/main/playground/PostgresEndToEnd

While debugging I noticed that the database was only being created in the first Postgres server. Upon further inspection it looks like all the various DbContext types that are being injected into the / route have the wrong connection string applied (except the first one).

This is probably a bug in the registration of the underlying Postgres connection in DI.

@dotnet-issue-labeler dotnet-issue-labeler bot added the area-app-model Issues pertaining to the APIs in Aspire.Hosting, e.g. DistributedApplication label Feb 6, 2024
@mitchdenny mitchdenny added area-integrations Issues pertaining to Aspire Integrations packages and removed area-app-model Issues pertaining to the APIs in Aspire.Hosting, e.g. DistributedApplication labels Feb 6, 2024
@mitchdenny mitchdenny added this to the preview 4 (Mar) milestone Feb 6, 2024
@mitchdenny
Copy link
Member Author

Looks like this impacts other Aspire EF components as well.

@eerhardt
Copy link
Member

eerhardt commented Feb 6, 2024

This looks like a duplicate of npgsql/efcore.pg#2891.

cc @roji

@roji
Copy link
Member

roji commented Feb 6, 2024

Yep, agree with @eerhardt that this look like a dup of npgsql/efcore.pg#2891.

Can you please try creating the NpgsqlDataSource yourself, and then explicitly passing that to AddNpgsqlDbContext rather than having it implicitly taken from DI? That should work around the problem for now (and confirm that we're discussing the same bug).

@eerhardt
Copy link
Member

eerhardt commented Feb 6, 2024

Looks like this impacts other Aspire EF components as well.

I haven't seen this affect other EF components. Can you share a repro of it affecting a non-Npgsql EF component?

@eerhardt
Copy link
Member

eerhardt commented Feb 6, 2024

This looks like a duplicate of npgsql/efcore.pg#2891.

Looking at @mitchdenny's fix in #2097, I don't think this is same issue. The problem is that we are injecting an NpgsqlDataSource into DI in order to work around npgsql/efcore.pg#2821. However, when you try adding multiple Npgsql DbContexts, we keep overwriting the NpgsqlDataSource in DI, since they aren't "keyed".

@roji - is there another way we can work around this bug? (Or even get the underlying bug fixed?) I don't really like that we are registering NpgsqlDataSource in DI when users add Npgsql EF. Especially since users can start depending on that DI service being registered. With the fix in #2097, it is actually making a breaking change because if users were getting an NpgsqlDataSource from DI, their code would no longer work because it is now keyed on the DbContext type.

@mitchdenny
Copy link
Member Author

Looks like this impacts other Aspire EF components as well.

I haven't seen this affect other EF components. Can you share a repro of it affecting a non-Npgsql EF component?

This was my mistake. Only an issue in npgsql.

@github-actions github-actions bot locked and limited conversation to collaborators Apr 22, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-integrations Issues pertaining to Aspire Integrations packages
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants