✅ Accessing associated records through navigation properties
say I had a class like so (I'm using them with EF Core)
given an instance of
Project
, how would I go about getting the related FeedbackRecords
, ManufacturingInstructions
, ModelPrototypes
, and Sketches
? Do I have to go through dbContext? or is there a way to go straight through them model itself (kinda like the way Django does it)?11 Replies
uhh, am i missing something? given you have an instance of
Project
named proj1
, then you access FeedbackRecords
by doing proj1.FeedbackRecords
.You would need to load them first
I thought they were empty until loaded?
.Include()
that i didn't know
Or, ideally,
.Select()
the exact data you need into DTOsok i think i got it now. basically any db access would need to go through the dbContext, is that right?
Well, yes
Hard to get the data from database without... querying the database
var post = new Blogpost()
doesn't just automagically grab one from the dbin my defense Django's ORM lets you do it straight from the model itself via something like
Project.objects.all()
(i'm coming from a Python background)
but yeah i see how it's laid out now
thanksGuess it uses active record or something, then
But yeah, with EF you'd either include, or ideally select
or
The latter would fetch only the requested data
Instead of loading everything
ahh ok so that's how you do it. TIL
thanks