تخطَّ إلى المحتوى

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).

var blogs = await _db.Blogs.ToListAsync();
foreach (var blog in blogs)
{
Console.WriteLine(blog.Posts.Count); // extra query each loop
}
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().