Rule Definition
For scenarios where entities are read but not updated, consider using the AsNoTracking method. This informs EF that the entities are not being modified, eliminating the need for change tracking and improving performance.
With EF and EF Core, the AsNoTracking method provides a simple yet powerful means to enhance the speed of read operations. By using AsNoTracking, you can reduce memory overhead, speed up query execution, minimize network traffic, and improve the overall scalability of your application.
This option allows EF to turn off the object cache and unnecessary identity management of the objects.
Applies to Entity Framework 5.0.0 and Entity Framework 4.3.1
Remediation
Use the AsNoTracking method.
Violation Code Sample
var orders = dbContext.Orders.ToList(); // ⚠️ VIOLATION
Fixed Code Sample
var orders = dbContext.Orders.AsNoTracking().ToList(); // ✓ FIXED
Reference
# Corner - Optimizing Performance in C# Entity Framework - 4. Batch Processing with AsNoTracking
https://www.c-sharpcorner.com/article/optimizing-performance-in-c-sharp-entity-framework/
MS Entity Framework Core - Tracking vs. No-Tracking Queries
https://learn.microsoft.com/en-us/ef/core/querying/tracking#no-tracking-queries
Enhancing Database Query Performance with Entity Framework Core: Real-world Examples and Performance Metrics
Example 11: NoTracking for Read-Only Queries
https://mohamed-hendawy.medium.com/enhancing-database-query-performance-with-entity-framework-core-real-world-examples-and-3899f285e67c
Related Technologies
Technical Criterion
Efficiency - SQL and Data Handling Performance
About CAST Appmarq
CAST Appmarq is by far the biggest repository of data about real IT systems. It's built on thousands of analyzed applications, made of 35 different technologies, by over 300 business organizations across major verticals. It provides IT Leaders with factual key analytics to let them know if their applications are on track.