Profile queries with ToQueryString(). Example: var data = _context.Products.AsNoTracking().Select(p => new { p.Name, p.Price }).ToList(); ⚡ 18. How to use compiled queries?
Compiled queries improve performance by caching query translation.
Example:
private static readonly Func<AppDbContext, decimal,
IEnumerable<Product>> _getExpensiveProducts =
EF.CompileQuery((AppDbContext ctx, decimal price) =>
ctx.Products.Where(p => p.Price > price));
var result = _getExpensiveProducts(_context, 1000);
🔍 19. What is global query filtering?
It allows you to apply filters automatically to all queries for a given entity — useful for soft
deletes or multi-tenancy.
Example:
Follow :
modelBuilder.Entity<Product>()
.HasQueryFilter(p => !p.IsDeleted);
All queries automatically exclude deleted products.
🌐 20. How do you handle database connection
pooling?
Connection pooling is handled automatically by ADO.NET and EF Core providers.
Each new DbContext reuses existing connections from the pool to reduce overhead.
For fine control:
options.UseSqlServer(connectionString, opt =>
opt.EnableRetryOnFailure());
Tips:
- Keep DbContexts short-lived (Scoped lifetime).
- Avoid keeping connections open unnecessarily.
Follow :
- For high-traffic apps, tune pool
settings in the connection string:
Max Pool Size=200; Min
Pool Size=5;
Authentication & Authorization