Lazy loading in loop (ARCH206)
هذا المحتوى غير متوفر بلغتك بعد.
Key dotnet.ef.lazy-loading-in-loop · Severity warning · Category ef
With lazy loading enabled, each navigation access inside a loop silently triggers an extra query.
Incorrect
Section titled “Incorrect”var customers = await _db.Customers.ToListAsync();foreach (var c in customers){ total += c.Orders.Sum(o => o.Total); // lazy load per customer}Correct
Section titled “Correct”var customers = await _db.Customers .Include(c => c.Orders) .ToListAsync();foreach (var c in customers){ total += c.Orders.Sum(o => o.Total);}Pre-load related collections with Include().