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

Make NpgsqlDataSource a scoped service instead of singleton #3086

Open
roji opened this issue Feb 9, 2024 · 1 comment
Open

Make NpgsqlDataSource a scoped service instead of singleton #3086

roji opened this issue Feb 9, 2024 · 1 comment
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@roji
Copy link
Member

roji commented Feb 9, 2024

NpgsqlDataSource is currently registered as a singleton service; although this conceptually makes sense, it creates various issues:

  • When automatically getting the data source from DI, there's no way in EF to include the data source instance in the decision-making on whether a new service provider is needed, causing interference between service providers (Using NpgsqlDataSource in DependencyInjection pollutes other containers #2891).
  • In some multi-tenant and testing scenarios, it's desirable to use many data sources with potentially the same EF service provider. In the current situation, this requires lots of EF service providers, which triggers our "many service providers" warning.

The main thing blocking making NpgsqlDataSource scoped is the fact that we get enum mappings from it in NpgsqlTypeMappingSource, which is itself a singleton. We should stop doing that (#3063), at which point it should be easy to make NpgsqlDataSource scoped.

@gbd3-en
Copy link

gbd3-en commented Feb 15, 2024

Excellent! We're in that multi-tenant scenario. Despite careful re-use & disposal of the NpgsqlDataSoruce and DbContext instances, we still end up with that ManyServiceProvidersCreatedWarning error.

Here is the full error in case it helps others searching for solutions:

'Microsoft.EntityFrameworkCore.Infrastructure.ManyServiceProvidersCreatedWarning':
More than twenty 'IServiceProvider' instances have been created for internal use by Entity Framework. This is commonly caused by injection of a new singleton service instance into every DbContext instance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants