Pooling, compiled queries and compiled models
DbContext pooling (AddDbContextPool)
For applications that frequently create and dispose DbContext instances, which is common in ASP.NET Core request handling, DbContext pooling can improve performance. It reuses DbContext instances from a pool instead of creating new ones each time, which reduces the overhead of DbContext initialization.
It’s important to understand that DbContext pooling is distinct from database connection pooling. The former reuses DbContext objects in application memory. The latter, managed by the ADO.NET provider, reuses physical database connections.
Compiled queries (EF.CompileQuery, EF.CompileAsyncQuery)
For frequently executed, performance-critical LINQ queries, EF Core lets you pre-compile them into .NET delegatesDelegatesA type-safe function pointer, so a method can be passed around like any other value.Opens in a new tab. Invoking these delegates bypasses the LINQ query compilation and caching mechanism, which can give a small performance boost for “hot path” queries.
EF Core’s built-in query caching is generally efficient, though. Use compiled queries judiciously, after profiling identifies a bottleneck.
Compiled models
Compiled models are a different feature from compiled queries. EF Core generates real C# code at build time that represents your model. That skips the reflection that normally runs at startup to build it (see the model noteORMs, DbContext and entitiesWhat EF Core is, and the four ideas its API is built on.Opens in a new tab), which matters most for large schemas. It’s the same idea as a source generatorSource generators versus reflectionA source generator does at build time what reflection does at runtime.Opens in a new tab: do the work at build time, not at runtime.
Database connection pooling
EF Core itself doesn’t implement connection pooling. It relies on the underlying ADO.NET database provider, for example SqlClient for SQL Server, to manage a pool of database connections. That significantly reduces the overhead of repeatedly opening and closing physical connections to the database.
All of these, with the query habits, are in one tableEF Core performance techniques at a glanceEach technique, when to use it, and the line of code that turns it on.Opens in a new tab.
Sources
- Advanced Performance Topics, EF Core, Microsoft Learn · accessed May 16, 2025