N plus one (ARCH203)
هذا المحتوى غير متوفر بلغتك بعد.
Key dotnet.ef.n-plus-one · Severity warning · Category ef
Accessing a related entity on each iteration runs one extra query per row in addition to the main query (the N+1 problem).
Incorrect
Section titled “Incorrect”var blogs = await _db.Blogs.ToListAsync();foreach (var blog in blogs){ Console.WriteLine(blog.Posts.Count); // extra query each loop}Correct
Section titled “Correct”var blogs = await _db.Blogs .Include(b => b.Posts) .ToListAsync();foreach (var blog in blogs){ Console.WriteLine(blog.Posts.Count);}Eager-load the related data once with Include().