-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Expose public API to reset a state of DbContext instance #15577
Comments
@dasMulli Have you tested that context pooling improves your perf? (It can, but only in limited scenarios. Most of the time the difference is barely measurable.) |
I'll get some more measurements next week. |
@dasMulli Typically when doing nothing else with EF other than returning a single entity instance from a single, simple no-tracking query, at very high throughput. |
Well.. looks like the app in question is doing that. Will look into more detailed measurement. |
@dasMulli In my own research, there appears to be several different knobs to turn to figure out optimal throughput. See for example this wonderful StackOverflow answer about using the Async vs. Synchronous APIs for SqlClient: https://stackoverflow.com/a/42419159/1040437 I have been slowly cataloging over the last 2+ years a lot of such war stories, so let me know how you make out. I've personally started using benchmarkDotNet and it's a wonderful tool for simulating some assumptions. While it's artificial in some regards (you're not creating sockets over a network of clients), its reproducible and hands down that wins over minor artificiality. |
Notes from triage: putting this on the backlog to consider exposing this. Specifically:
|
Experience from the 3.0 release (see #17784 #18406) indicates that people are doing mass-detach of all entities because they need to clean the context and can't create a new instance. This adds some evidence to the need for this since it seems we're fighting a losing battle telling people not to keep their context instances around. |
@ajcvickers I think the main reason you are losing the battle is their is no clear DI pattern for most end-users. This is why DbContextScope GitHub project gained popularity in EF6 .NET Framework paradigm. What I've done in the past is facilitated DI of You lose when two things happen:
|
@jzabroski Can you elaborate on how you're accomplishing the DbContextScope paradigm using EF Core 3 now? My team has been using DbContextScope up through EF Core 2.2, but with EF Core 3, some of the essential parts have gone away within EF. So we don't have a direct upgrade path to EF Core 3 from 2.2. With 2.2, we registered IAmbientDbContextLocator and IDbContextScopeFactory with the .NET Core DI system, and all our classes get those injected like magic. Is there a standard way in EF Core that should be used to just let the DI in .NET Core manage the scopes and handle SaveChanges() at the scope level at the outter-most SaveChanges() call in a scope, just like DbContextScope does it or you? SOS. Send help immediately. 🆘 |
I don't actually use DbContextScope - I use dependency injection to ensure scopes. Autofac has the concept of Delegate Factories, and other contains have similar concepts. (Tangent: I actually don't like DbContextScope, as it combines too many concerns, and I like narrow interfaces when creating a facade around a much bigger system.) In general, I agree with @ajcvickers decision to tell people not to keep their contexts around. But then the question becomes, what's the cheapest way to get a new context? You need a new DI Scope. See: Note, I deliberately use On some projects, I have As for why I don't always abstract things into the most narrow interfaces with the most narrow concerns? I don't always believe in SOLID, especially Single Responsibility Principle. All I care about is that I can write tests that define properties of the interface for all classes that implement that interface. |
Related: #18575 |
Fixes #15577 This is an alternative to mass-detach for situations where creating a new context instance is difficult. The context configuration is not reset, since it seems likely that setting like lazy-loading and registered events are more useful left as they would be--just as is the case when doing mass-detach.
Fixes #15577 This is an alternative to mass-detach for situations where creating a new context instance is difficult. The context configuration is not reset, since it seems likely that setting like lazy-loading and registered events are more useful left as they would be--just as is the case when doing mass-detach.
Fixes #15577 This is an alternative to mass-detach for situations where creating a new context instance is difficult. The context configuration is not reset, since it seems likely that setting like lazy-loading and registered events are more useful left as they would be--just as is the case when doing mass-detach.
pooling db context instances is a great way to improve performance but it is only accessible when using DI contexts.
When migrating existing applications from EF classic to EF Core, it may be beneficial to use DbContextPool directly, since the application may not use DI or use different approaches (esp. with regard to scoping) to DI not covered by the EF-provided extension method for dependency injection configuration.
By making
DbContextPool<TContext>
a public API (instead of pubinternal), this would allow existing (large) applications to use pooling without the fear of APIs changing (a lot), even though it means that using it correctly could be an issue - but this is the same for correctly using types likeArrayPool<T>
.The text was updated successfully, but these errors were encountered: