Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,10 @@

return service;
}

/// <summary>
/// Clears the internal Entity Framework service provider cache.
/// </summary>
public static void ClearServiceProviderCache(this DatabaseFacade databaseFacade)
=> EntityFrameworkMetrics.ClearServiceProviderCache();

Check failure on line 55 in src/EFCore/Infrastructure/Internal/InfrastructureExtensions.cs

View check run for this annotation

Azure Pipelines / efcore-ci (Build Linux)

src/EFCore/Infrastructure/Internal/InfrastructureExtensions.cs#L55

src/EFCore/Infrastructure/Internal/InfrastructureExtensions.cs(55,12): error CS1929: (NETCORE_ENGINEERING_TELEMETRY=Build) 'EntityFrameworkMetrics' does not contain a definition for 'ClearServiceProviderCache' and the best extension method overload 'InfrastructureExtensions.ClearServiceProviderCache(DatabaseFacade)' requires a receiver of type 'Microsoft.EntityFrameworkCore.Infrastructure.DatabaseFacade'

Check failure on line 55 in src/EFCore/Infrastructure/Internal/InfrastructureExtensions.cs

View check run for this annotation

Azure Pipelines / efcore-ci (Build macOS)

src/EFCore/Infrastructure/Internal/InfrastructureExtensions.cs#L55

src/EFCore/Infrastructure/Internal/InfrastructureExtensions.cs(55,12): error CS1929: (NETCORE_ENGINEERING_TELEMETRY=Build) 'EntityFrameworkMetrics' does not contain a definition for 'ClearServiceProviderCache' and the best extension method overload 'InfrastructureExtensions.ClearServiceProviderCache(DatabaseFacade)' requires a receiver of type 'Microsoft.EntityFrameworkCore.Infrastructure.DatabaseFacade'

Check failure on line 55 in src/EFCore/Infrastructure/Internal/InfrastructureExtensions.cs

View check run for this annotation

Azure Pipelines / efcore-ci

src/EFCore/Infrastructure/Internal/InfrastructureExtensions.cs#L55

src/EFCore/Infrastructure/Internal/InfrastructureExtensions.cs(55,12): error CS1929: (NETCORE_ENGINEERING_TELEMETRY=Build) 'EntityFrameworkMetrics' does not contain a definition for 'ClearServiceProviderCache' and the best extension method overload 'InfrastructureExtensions.ClearServiceProviderCache(DatabaseFacade)' requires a receiver of type 'Microsoft.EntityFrameworkCore.Infrastructure.DatabaseFacade'
}
8 changes: 8 additions & 0 deletions src/EFCore/Internal/ServiceProviderCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -200,4 +200,12 @@ private static bool ApplyServices(IDbContextOptions options, ServiceCollection s

return false;
}

/// <summary>
/// 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.
/// </summary>
public virtual void Clear() => _configurations.Clear();
}
17 changes: 17 additions & 0 deletions test/EFCore.Tests/ServiceProviderCacheTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -327,4 +328,20 @@ public override void PopulateDebugInfo(IDictionary<string, string> 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);
}
}
Loading