EF Core Optimization Strategies and Migration Guide
L2 Cache Relevance in EF Core
L1 Cache in EF Core is scoped to the DbContext instance. L2 (second-level) cache, however, shares cached
results across DbContext instances or across sessions.
EF Core does not provide built-in L2 cache but supports it via libraries like
EFCoreSecondLevelCacheInterceptor.
Best uses for L2 cache:
- Lookup/reference data
- Read-heavy entities
- Global or frequently accessed configuration data
Setup involves:
- Installing EFCoreSecondLevelCacheInterceptor
- Registering cache (MemoryCache or Redis)
- Using `.Cacheable()` on queries
Migration from Telerik OpenAccess to EF Core
Telerik OpenAccess is now discontinued. EF Core offers a modern alternative with better performance, LINQ
support, and extensibility.
Migration involves:
1. Exporting entity models
2. Creating EF Core DbContext
3. Rewriting queries using EF LINQ
4. Replacing ObjectScope with EF?s DbContext
5. Replacing OpenAccess-specific patterns like FetchStrategy
6. Adding cache with EFCoreSecondLevelCacheInterceptor
Page 1
EF Core Optimization Strategies and Migration Guide
L2 Cache Best Practices for Enterprise-Scale Apps
Use L2 cache only for:
- Static or rarely changing data
- Shared or global configuration
- Frequently queried read-only data
Do:
- Use Redis/NCache in multi-node environments
- Apply per-query TTLs
- Invalidate cache on update/delete
- Monitor memory and performance impact
Don't:
- Cache real-time or volatile data
- Cache user/tenant-specific sensitive data
- Over-cache navigation properties or entire object graphs
Alternatives to L2 Cache
1. Compiled Queries:
- Use EF.CompileQuery for hot-path queries
- Reduces LINQ translation overhead
2. CQRS + Read Models:
- Separate read/write concerns
- Use denormalized projections for fast reads
Page 2
EF Core Optimization Strategies and Migration Guide
3. HTTP Response Caching:
- Use ResponseCache for GET APIs
- Works well with CDNs and reverse proxies
4. Distributed In-Memory Cache:
- Use Redis/NCache for DTOs or static data
- Not tied to EF
5. Materialized Views:
- Precomputed views for heavy joins/aggregations
- Refreshable and read-optimized
6. Elasticsearch:
- For fast search, logs, analytics
- Scalable and powerful indexing
Learning Resources
Videos:
- CQRS with EF Core ? Milan Jovanovic (YouTube)
- EF Core Compiled Queries ? Code Maze
- Redis Cache with EF Core ? CodeWithMukesh
Articles:
- Unleash EF Core Performance ? milanjovanovic.tech
- Distributed Caching in ASP.NET Core ? Microsoft Docs
- Applying CQRS in ASP.NET Core ? Telerik Blog
- Boosting Performance with Materialized Views ? Hashnode Blog
These guides provide hands-on steps and sample code to implement advanced caching and optimization
Page 3
EF Core Optimization Strategies and Migration Guide
strategies in EF Core.
Page 4