❔ Ef core relations
Hey, im working on a simple project and im getting so much confused about how efcore works.
I have two simple classes, where one have List of second one, and when i want to acces this list its always null.
14 Replies
it works when i do something like this
but i think its really bad if i need to do this
Delete that
.ToList()
And, yes, generally speaking using a .Select()
is preferable to .Include()
i think u didnt understand me. cs
var requestedQuestionnaire = _context.Questionnaires
.FirstOrDefault(x => x.Id == Id);
foreach(var option in requestedQuestionnaire.Options)
{
// do some stuff
}
```
i cant do that because options is null
You want just the options?
If you want it all, you can still do
Bonus points if you make it async
yea i get it but i done it before and i didnt have to use Include()
Then you probably had lazy loading turned on
Which you shouldn't do
hmm maybe idk
Lazy loading queries the database each time a navigation property is accessed
Eager loading with
.Include()
or better yet, .Select()
, only queries the database once and gets all the dataokay thanks ❤️
could you tell me how to do it with select?
You would create a new class, or multiple classes, that contain only the properties you need
And select into those
Big thanks for you ❤️
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.