|
2 | 2 | title: Advanced Performance Topics - EF Core |
3 | 3 | description: Advanced performance topics for Entity Framework Core |
4 | 4 | author: roji |
5 | | -ms.date: 9/26/2023 |
| 5 | +ms.date: 11/21/2025 |
6 | 6 | uid: core/performance/advanced-performance-topics |
7 | 7 | --- |
8 | 8 | # Advanced Performance Topics |
@@ -151,6 +151,9 @@ Note that there is no need to parameterize each and every query: it's perfectly |
151 | 151 |
|
152 | 152 | > [!NOTE] |
153 | 153 | > EF Core's [metrics](xref:core/logging-events-diagnostics/metrics) report the Query Cache Hit Rate. In a normal application, this metric reaches 100% soon after program startup, once most queries have executed at least once. If this metric remains stable below 100%, that is an indication that your application may be doing something which defeats the query cache - it's a good idea to investigate that. |
| 154 | +> |
| 155 | +> [!TIP] |
| 156 | +> EF Core uses `IMemoryCache` for internal caching of compiled queries and models. You can configure the cache size limit if needed - see [Memory Cache Integration](#memory-cache-integration) for more information. |
154 | 157 |
|
155 | 158 | > [!NOTE] |
156 | 159 | > How the database manages caches query plans is database-dependent. For example, SQL Server implicitly maintains an LRU query plan cache, whereas PostgreSQL does not (but prepared statements can produce a very similar end effect). Consult your database documentation for more details. |
@@ -319,3 +322,19 @@ As with any layer, EF Core adds a bit of runtime overhead compared to coding dir |
319 | 322 | * Consider disabling thread safety checks by setting `EnableThreadSafetyChecks` to false in your context configuration. |
320 | 323 | * Using the same `DbContext` instance concurrently from different threads isn't supported. EF Core has a safety feature which detects this programming bug in many cases (but not all), and immediately throws an informative exception. However, this safety feature adds some runtime overhead. |
321 | 324 | * **WARNING:** Only disable thread safety checks after thoroughly testing that your application doesn't contain such concurrency bugs. |
| 325 | + |
| 326 | +## Memory Cache Integration |
| 327 | + |
| 328 | +EF Core integrates with ASP.NET Core's memory caching infrastructure through `IMemoryCache`. However, this is not used for the internal service provider caching. |
| 329 | + |
| 330 | +EF Core automatically configures `IMemoryCache` with a default size limit of 10240 for internal caching operations such as query compilation and model building. You should call `AddMemoryCache` if you need to change these defaults. For reference, a compiled query has a cache size of 10, while the built model has a cache size of 100. |
| 331 | + |
| 332 | +```csharp |
| 333 | +public void ConfigureServices(IServiceCollection services) |
| 334 | +{ |
| 335 | + services.AddMemoryCache(options => options.SizeLimit = 20480); // Custom size limit for EF Core caching |
| 336 | + |
| 337 | + services.AddDbContext<ApplicationDbContext>(options => |
| 338 | + options.UseSqlServer(connectionString)); |
| 339 | +} |
| 340 | +``` |
0 commit comments