-
Notifications
You must be signed in to change notification settings - Fork 2k
Document EnableServiceProviderCaching and UseMemoryCache configuration in EF Core #5116
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
samples/core/Miscellaneous/CachingConfiguration/CachingConfiguration.csproj
Outdated
Show resolved
Hide resolved
Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
…arios Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
|
@copilot Update the PR description with a summary of the changes |
Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
Updated the PR description with a comprehensive summary of the changes, including what was documented, the technical details, and the documentation structure. |
| - Better performance for applications that create many `DbContext` instances | ||
| - Lower memory overhead when multiple contexts share configurations | ||
|
|
||
| **When to disable caching**: You might want to disable service provider caching (`false`) for testing environments to ensure each test gets a fresh service provider, or when `DbContext` configurations change dynamically at runtime. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| **When to disable caching**: You might want to disable service provider caching (`false`) for testing environments to ensure each test gets a fresh service provider, or when `DbContext` configurations change dynamically at runtime. | |
| **When to disable caching**: You might want to disable service provider caching (`false`) for testing environments to ensure each test gets a fresh service provider when `DbContext` configurations change test-to-test. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated the guidance in commit 705ef58 to focus on testing environments where DbContext configurations change test-to-test.
Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
| services.AddDbContext<ApplicationDbContext>(options => | ||
| options | ||
| .UseSqlServer(connectionString) | ||
| .EnableServiceProviderCaching(true)); // Default, but shown for clarity |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a duplicate snippet of the one shown just above, but with true instead of false. It's also out of context - the true snippet is shown right after the sentence about how to disable caching (the opposite).
|
|
||
| Using dependency injection, this can be achieved by either registering the context as scoped, and creating scopes (using `IServiceScopeFactory`) for each thread, or by registering the `DbContext` as transient (using the overload of `AddDbContext` which takes a `ServiceLifetime` parameter). | ||
|
|
||
| ## EnableServiceProviderCaching |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm conflicted about this sort of documentation. Yes, we have EnableServiceProviderCaching() as a public API, but this is something that I think we'd never recommend users ever actually use
So I'm not sure whether (and why) we should actually be documenting this - it runs the risk of pushing people towards an API they shouldn't use, it makes this doc page less useful (since it gets bloated with a non-useful API), and it creates more work for us (writing/reviewing the docs).
If we believe there are legitimate reasons for users to use this (I'm not really aware of any), then these should be documented here. There's just one sentence below: "for testing environments to ensure each test gets a fresh service provider when DbContext configurations change test-to-test", but I'm unclear as to when users would actually run into this: most settings don't require a different service provider, and those that do generally should be singleton options, so a new service provider is created anyway.
| public void ConfigureServices(IServiceCollection services) | ||
| { | ||
| services.AddMemoryCache(options => | ||
| { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: remove unneeded curlies
| } | ||
| ``` | ||
|
|
||
| ## Memory Cache Integration |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This feels like it belongs in the advanced performance topics page, not here. We should ideally be documenting by concern (modeling, performance, querying...).
Summary
This PR adds comprehensive documentation for EF Core's memory caching configuration options, specifically
EnableServiceProviderCachingandUseMemoryCache. The documentation explains where these features are used, what the defaults are, and when to customize them.What's Documented
EnableServiceProviderCaching
true) - service providers are cached and reused acrossDbContextinstances with the same configurationDbContextconfigurations change test-to-testDbContextinstancesUseMemoryCache / Memory Cache Integration
IMemoryCacheused by EF Core for internal caching operations like query compilation and model buildingIMemoryCachewith a default size limit of 10240AddMemoryCacheif you need to change the default size limitsIMemoryCacheis not used for internal service provider caching (separate feature)Documentation Changes
Updated Files
EnableServiceProviderCachingandUseMemoryCacheto the DbContext configuration options table## EnableServiceProviderCachingsection with complete explanation of service provider caching## Memory Cache Integrationsection explainingIMemoryCacheconfigurationDocumentation Structure
##heading level for clear organizationKey Technical Details
Fixes #4855
Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.