CRITICAL
Rule Definition
When you must include multiple collections (e.g., one-to-many or many-to-many), avoid a single huge JOIN by telling EF to use separate SQL queries.
Remediation
Only load what you actually need. Don’t include navigation properties unless they are required by your logic or UI.
You can also project to a DTO
Violation Code Sample
// VIOLATION, multiple Include() + conversion
var blogs = dbContext.Blogs
.Include(b => b.Posts)
.Include(b => b.Tags)
.ToList(); // ⚠️ VIOLATION : AsSplitQuery() not used
Fixed Code Sample
// NO VIOLATION with local use of AsSplitQuery option
var blogs = dbContext.Blogs
.Include(b => b.Posts)
.Include(b => b.Tags)
.AsSplitQuery() // ✓ FIXED
.ToList();
// OR NO VIOLATION if global setting is enabled for DbContext
optionsBuilder.UseSqlServer(connectionString, o =>
o.UseQuerySplittingBehavior(QuerySplittingBehavior.SplitQuery)); // ✓ FIXED
Reference
https://learn.microsoft.com/en-us/ef/core/querying/related-data/eager
https://learn.microsoft.com/en-us/ef/core/querying/single-split-queries
https://learn.microsoft.com/en-us/ef/core/performance/
Related Technologies
Technical Criterion
CWE-664 - Improper Control of a Resource Through its Lifetime [Pillar]
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.