diff --git a/src/EFCore/Infrastructure/Internal/EntityFrameworkMetrics.cs b/src/EFCore/Infrastructure/Internal/EntityFrameworkMetrics.cs index 0c071f45891..5f779b65daa 100644 --- a/src/EFCore/Infrastructure/Internal/EntityFrameworkMetrics.cs +++ b/src/EFCore/Infrastructure/Internal/EntityFrameworkMetrics.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. using System.Diagnostics.Metrics; +using Microsoft.EntityFrameworkCore.Internal; namespace Microsoft.EntityFrameworkCore.Infrastructure.Internal; diff --git a/src/EFCore/Infrastructure/Internal/InfrastructureExtensions.cs b/src/EFCore/Infrastructure/Internal/InfrastructureExtensions.cs index b593c915ee6..7cdd27dff43 100644 --- a/src/EFCore/Infrastructure/Internal/InfrastructureExtensions.cs +++ b/src/EFCore/Infrastructure/Internal/InfrastructureExtensions.cs @@ -47,4 +47,10 @@ public static object GetService(IInfrastructure accessor, Type return service; } + + /// + /// Clears the internal Entity Framework service provider cache. + /// + public static void ClearServiceProviderCache(this DatabaseFacade databaseFacade) + => EntityFrameworkMetrics.ClearServiceProviderCache(); } diff --git a/src/EFCore/Internal/ServiceProviderCache.cs b/src/EFCore/Internal/ServiceProviderCache.cs index 876eae80d24..347f8c7d375 100644 --- a/src/EFCore/Internal/ServiceProviderCache.cs +++ b/src/EFCore/Internal/ServiceProviderCache.cs @@ -200,4 +200,12 @@ private static bool ApplyServices(IDbContextOptions options, ServiceCollection s return false; } + + /// + /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to + /// the same compatibility standards as public APIs. It may be changed or removed without notice in + /// any release. You should only use it directly in your code with extreme caution and knowing that + /// doing so can result in application failures when updating to a new Entity Framework Core release. + /// + public virtual void Clear() => _configurations.Clear(); } diff --git a/test/EFCore.Tests/ServiceProviderCacheTest.cs b/test/EFCore.Tests/ServiceProviderCacheTest.cs index 3311addf1a2..5826e7cb971 100644 --- a/test/EFCore.Tests/ServiceProviderCacheTest.cs +++ b/test/EFCore.Tests/ServiceProviderCacheTest.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. using Microsoft.EntityFrameworkCore.Diagnostics.Internal; +using Microsoft.EntityFrameworkCore.Infrastructure.Internal; using Microsoft.EntityFrameworkCore.Internal; namespace Microsoft.EntityFrameworkCore; @@ -327,4 +328,20 @@ public override void PopulateDebugInfo(IDictionary debugInfo) => debugInfo["Fake2"] = "1"; } } + + [ConditionalFact] + public void Service_provider_cache_can_be_cleared() + { + var cache = ServiceProviderCache.Instance; + var options = new DbContextOptionsBuilder().UseInMemoryDatabase("TestDB").Options; + + cache.GetOrAdd(options, providerRequired: false); + + EntityFrameworkMetrics.ClearServiceProviderCache(); + + var field = typeof(ServiceProviderCache).GetField("_configurations", BindingFlags.NonPublic | BindingFlags.Instance); + var dict = (System.Collections.IDictionary)field.GetValue(cache); + + Assert.Equal(0, dict.Count); + } }