Skip to content

Memory Cache Integration snippet is misleading #5271

@PashaPash

Description

@PashaPash

Type of issue

Code doesn't work

Description

The snippet about changing the default cache size is misleading and misses UseMemoryCache, which you need to make EF use your custom cache instance.

Current snippet:

public void ConfigureServices(IServiceCollection services)
{
    services.AddMemoryCache(options => options.SizeLimit = 20480); // Custom size limit for EF Core caching
    
    services.AddDbContext<ApplicationDbContext>(options =>
        options.UseSqlServer(connectionString));
}

Correct version:

public void ConfigureServices(IServiceCollection services)
{
    services.AddMemoryCache(options => options.SizeLimit = 20480); // Custom size limit for EF Core caching

    services.AddDbContext<TContext>((serviceProvider, options) =>
    {
        options.UseMemoryCache(serviceProvider.GetRequiredService<IMemoryCache>());
        ///...
    });
}

Page URL

https://learn.microsoft.com/en-us/ef/core/performance/advanced-performance-topics?tabs=with-di%2Cexpression-api-with-constant#memory-cache-integration

Content source URL

https://github.com/dotnet/EntityFramework.Docs/blob/main/entity-framework/core/performance/advanced-performance-topics.md

Document Version Independent Id

e96b453f-0256-8bc9-bd21-1343f2fe246c

Platform Id

ada64a4c-83c3-6714-0fa4-d80a203f4349

Article author

@roji

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions