C
C#2w ago
Faker

✅ How does explicit loading differs from lazy loading when using EF Core

Hello guys, I was reading about the methods used to load data in EF Core. I came across eager loading, explicit loading and lazy loading. I understood that eager loading is loading data directly with the query, while lazy loading is loading the data as we go, like in a for-each loop. But I didn't understand the explicit loading can someone explain please. If explicit loading is loading data when we need, isn't it the same thing as lazy loading?
24 Replies
Angius
Angius2w ago
Never heard of explicit loading, at least not in the context of eager/lazy Only thing that comes to mind would be maybe .Select()ing into a DTO, to explicitly choose which columns to load?
jcotton42
jcotton422w ago
@Faker Explicit loading must be done, well, explicitly. Lazy is implicit, and always sync.
Angius
Angius2w ago
Huh So it's basically "I really want to use lazy loading, but I really don't want to use lazy loading"
jcotton42
jcotton422w ago
"I need to load conditionally after the fact," I guess.
Faker
FakerOP2w ago
hmm is there any use case of explicit loading?
jcotton42
jcotton422w ago
Eager: you do things right. Explicit: weird edge case. Lazy: you hate your users.
Angius
Angius2w ago
Basically lol Tbh I can't really see the much use for explicit loading, so it's hard for me to say what example usage would be
Faker
FakerOP2w ago
yep, will just stick with eager / lazy loading for now but at least I know it exist :kekw:
Angius
Angius2w ago
Maybe something like
var post = await _ctx.Posts.FirstOrDefaultAsync(p => p.Id == id);
if (data.NewTags is { Length: > 0 })
{
await _ctx.Entry(post)
.Collection(p => p.Tags)
.LoadAsync();
}
var post = await _ctx.Posts.FirstOrDefaultAsync(p => p.Id == id);
if (data.NewTags is { Length: > 0 })
{
await _ctx.Entry(post)
.Collection(p => p.Tags)
.LoadAsync();
}
so that you load the tags only when there are actual tags to edit?
jcotton42
jcotton422w ago
@Faker don't use lazy. is bad
Faker
FakerOP2w ago
ahh I see
Angius
Angius2w ago
Why lazy loading is bad:
foreach (var tag in post.Tags)
{
<div class="tag">@tag.Name</div>
}
foreach (var tag in post.Tags)
{
<div class="tag">@tag.Name</div>
}
Assuming Post has a lazy nav property to Tags, each loop here is a separate, synchronous, database query
Faker
FakerOP2w ago
I don't really use it in a foreach loop, like I do this:
C#
var instructors = context.Instructors.AsNoTracking().ToList();
foreach (var instructor in instructors)
{
Console.WriteLine(instructor);
}
C#
var instructors = context.Instructors.AsNoTracking().ToList();
foreach (var instructor in instructors)
{
Console.WriteLine(instructor);
}
like the foreach is lazy loading, but for eager loading, I think I need to use Include() in the fluent api ?
Angius
Angius2w ago
Or better yet, .Select()
Faker
FakerOP2w ago
yeah I see, we would have one roundtrip for each tags?
Angius
Angius2w ago
Yeah, if you can load the post and the tags in a single query Or better yet, the post, and the tag names since you're not using other tag properties
jcotton42
jcotton422w ago
@Faker that's not lazy loading.
Angius
Angius2w ago
Hence select
Faker
FakerOP2w ago
oh Select also loads data eagerly, that why we can kind of project things ?
Angius
Angius2w ago
ye .Include(), .ThenInclude() loads everything eagerly, .Select() loads only the selected columns eagerly
Faker
FakerOP2w ago
yeah sorry, the example that @Angius gave was more appropriate
Angius
Angius2w ago
SELECT * FROM tbl vs SELECT t.Name, t.Skunga FROM tbl t
Faker
FakerOP2w ago
AHHHH I see yeah It's clearer now, thanks guys 👍

Did you find this page helpful?