CRITICAL
Rule Definition
Calling Add() for each entity inside a loop causes:
- Repeated method calls
- Slower performance vs. batched AddRange()
Remediation
Use AddRange() when it is applicable
Violation Code Sample
foreach (var item in items)
{
dbContext.Entities.Add(item); // ⚠️ VIOLATION
}
Fixed Code Sample
// No violation when using the right API
dbContext.Entities.AddRange(items); // ✓ FIXED
Reference
https://learn.microsoft.com/en-us/ef/core/saving/basic#add-vs-addrange
Related Technologies
Technical Criterion
Efficiency - Expensive Calls in Loops
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.