You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Make NpgsqlDataSource a scoped service instead of singleton #3086
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.
Reacted by pinkfloydx33, Greg Brady, alienwareone, Paulus Pärssinen, David Sekula, nklth, Sean, Rasmus Hansen, Thomas De Craemer, Jason Sander Egan and 2 more
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.
@roji I see this issue as closed but we are still facing this issue in the efcore 9.0
System.InvalidOperationException: An error was generated for warning '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.
We have ensured that in our multi-tenant system, NpgsqlDataSource is cached so each tenant has exactly one NpgsqlDataSource. Here is our code:
publicstaticclassNpgsqlConfiguration{privatestaticreadonlyAction<NpgsqlDbContextOptionsBuilder>_builderOptions= builderOptions =>{builderOptions.UseNodaTime().MapEnum<ViewPermission>();};[Cache(ProfileName=PostSharpCachingProfile.ImmutableCache)]publicstaticNpgsqlDataSourceGetNpgsqlDataSource(stringconnectionString){vardataSourceBuilder=newNpgsqlDataSourceBuilder(connectionString);// Logger for NpgsqldataSourceBuilder.UseLoggerFactory(Logging.GetLoggerFactory());dataSourceBuilder.UseNodaTime();dataSourceBuilder.MapEnum<ViewPermission>();returndataSourceBuilder.Build();}// Note that when using an external NpgsqlDataSource, you need to configure mappings and plugins // at both levels yourself - both on the NpgsqlDataSource and at the EF provider level.publicstaticAction<NpgsqlDbContextOptionsBuilder>GetNpgsqlBuilderOptions(){return_builderOptions;}}
OnConfiguring is executed for every DbContext instance, so GetNpgsqlDataSource gets called multiple times, and constructs a new data source each time. That's not what you want.
The recommendation since 9.0 is to use ConfigureDataSource rather than manage constructing the data source yourself and provide it (see 9.0 release notes). Otherwise, if you want to continue an external NpgsqlDataSource, construct it once outside and pass the same instance in.
Thanks, I missed that, I was still using the old method of configuring by creating datasources, just passing in the connection string and build options solved the issue.
Activity
gbd3-en commentedon 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
ManyServiceProvidersCreatedWarningerror.Here is the full error in case it helps others searching for solutions:
adnan-kamili commentedon Jan 29, 2025
@roji I see this issue as closed but we are still facing this issue in the efcore 9.0
System.InvalidOperationException: An error was generated for warning '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.We have ensured that in our multi-tenant system,
NpgsqlDataSourceis cached so each tenant has exactly one NpgsqlDataSource. Here is our code:Here is the NpgsqlConfiguration:
Any guidance you can provide?
roji commentedon Jan 30, 2025
OnConfiguring is executed for every DbContext instance, so GetNpgsqlDataSource gets called multiple times, and constructs a new data source each time. That's not what you want.
The recommendation since 9.0 is to use ConfigureDataSource rather than manage constructing the data source yourself and provide it (see 9.0 release notes). Otherwise, if you want to continue an external NpgsqlDataSource, construct it once outside and pass the same instance in.
adnan-kamili commentedon Jan 31, 2025
Thanks, I missed that, I was still using the old method of configuring by creating datasources, just passing in the connection string and build options solved the issue.