✅ 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
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?@Faker Explicit loading must be done, well, explicitly.
Lazy is implicit, and always sync.
Explicit Loading of Related Data - EF Core
Explicit loading of related data with Entity Framework Core
Huh
So it's basically "I really want to use lazy loading, but I really don't want to use lazy loading"
"I need to load conditionally after the fact," I guess.
hmm is there any use case of explicit loading?
Eager: you do things right.
Explicit: weird edge case.
Lazy: you hate your users.
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
yep, will just stick with eager / lazy loading for now but at least I know it exist :kekw:
Maybe something like
so that you load the tags only when there are actual tags to edit?
@Faker don't use lazy.
is bad
ahh I see
Why lazy loading is bad:
Assuming
Post
has a lazy nav property to Tags
, each loop here is a separate, synchronous, database queryI don't really use it in a foreach loop, like I do this:
like the foreach is lazy loading, but for eager loading, I think I need to use
Include()
in the fluent api ?Or better yet,
.Select()
yeah I see, we would have one roundtrip for each tags?
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
@Faker that's not lazy loading.
Hence select
oh
Select
also loads data eagerly, that why we can kind of project things ?ye
.Include()
, .ThenInclude()
loads everything eagerly, .Select()
loads only the selected columns eagerlyyeah sorry, the example that @Angius gave was more appropriate
SELECT * FROM tbl
vs SELECT t.Name, t.Skunga FROM tbl t
AHHHH
I see
yeah
It's clearer now, thanks guys 👍