diff --git a/src/EFCore/Internal/ServiceProviderCache.cs b/src/EFCore/Internal/ServiceProviderCache.cs index ca38174e49e..39c36795133 100644 --- a/src/EFCore/Internal/ServiceProviderCache.cs +++ b/src/EFCore/Internal/ServiceProviderCache.cs @@ -62,16 +62,26 @@ public virtual IServiceProvider GetOrAdd(IDbContextOptions options, bool provide if (coreOptionsExtension?.ServiceProviderCachingEnabled == false) { - return BuildServiceProvider(options, _configurations).ServiceProvider; + return BuildServiceProvider(options, (_configurations, options)).ServiceProvider; } - return _configurations.GetOrAdd(options, BuildServiceProvider, _configurations).ServiceProvider; + var cacheKey = options; + var extension = options.FindExtension(); + if (extension != null + && extension.ApplicationServiceProvider != null) + { + cacheKey = ((DbContextOptions)options).WithExtension(extension.WithApplicationServiceProvider(null)); + } + + return _configurations.GetOrAdd(cacheKey, BuildServiceProvider, (_configurations, options)).ServiceProvider; static (IServiceProvider ServiceProvider, IDictionary DebugInfo) BuildServiceProvider( - IDbContextOptions options, - ConcurrentDictionary DebugInfo)> - configurations) + IDbContextOptions _, + (ConcurrentDictionary DebugInfo)>, + IDbContextOptions) arguments) { + var (configurations, options) = arguments; + ValidateOptions(options); var debugInfo = new Dictionary();