✅ Accessing associated records through navigation properties

say I had a class like so (I'm using them with EF Core)
public class Project
{
public int Id { get; set; }

[Column(TypeName="VARCHAR(32)")]
public string Name { get; set; }
public bool Original { get; set; }
public int CreatedOn { get; set; }
public int UpdatedOn { get; set; }
public int DeletedOn { get; set; }

public List<FeedbackRecord> FeedbackRecords { get; set; }
public List<ManufacturingInstructions> ManufacturingInstructions { get; set; }
public List<ModelPrototype> ModelPrototypes { get; set; }
public List<Sketch> Sketches { get; set; }
}
public class Project
{
public int Id { get; set; }

[Column(TypeName="VARCHAR(32)")]
public string Name { get; set; }
public bool Original { get; set; }
public int CreatedOn { get; set; }
public int UpdatedOn { get; set; }
public int DeletedOn { get; set; }

public List<FeedbackRecord> FeedbackRecords { get; set; }
public List<ManufacturingInstructions> ManufacturingInstructions { get; set; }
public List<ModelPrototype> ModelPrototypes { get; set; }
public List<Sketch> Sketches { get; set; }
}
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
ero
ero4w ago
uhh, am i missing something? given you have an instance of Project named proj1, then you access FeedbackRecords by doing proj1.FeedbackRecords.
Angius
Angius4w ago
You would need to load them first
enrico11011
enrico11011OP4w ago
I thought they were empty until loaded?
Angius
Angius4w ago
.Include()
ero
ero4w ago
that i didn't know
Angius
Angius4w ago
Or, ideally, .Select() the exact data you need into DTOs
enrico11011
enrico11011OP4w ago
ok i think i got it now. basically any db access would need to go through the dbContext, is that right?
Angius
Angius4w ago
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 db
enrico11011
enrico11011OP4w ago
in 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 thanks
Angius
Angius4w ago
Guess it uses active record or something, then But yeah, with EF you'd either include, or ideally select
var post = await _ctx.Blogposts
.Where(p => p.Id == id)
.Include(p => p.Category)
.Include(p => p.Tags)
.FirstOrDefaultAsync();
var post = await _ctx.Blogposts
.Where(p => p.Id == id)
.Include(p => p.Category)
.Include(p => p.Tags)
.FirstOrDefaultAsync();
or
var post = await _ctx.Blogposts
.Where(p => p.Id == id)
.Select(p => new BlogpostDto {
Title = p.Title,
Body = p.Body,
CategoryName = p.Category.Name,
Tags = p.Tags.Select(t => new TagDto {
Name = t.Name,
Color = t.Color,
}),
Author = $"{t.Author.FirstName} {t.Author.LastName}"
})
.FirstOrDefaultAsync();
var post = await _ctx.Blogposts
.Where(p => p.Id == id)
.Select(p => new BlogpostDto {
Title = p.Title,
Body = p.Body,
CategoryName = p.Category.Name,
Tags = p.Tags.Select(t => new TagDto {
Name = t.Name,
Color = t.Color,
}),
Author = $"{t.Author.FirstName} {t.Author.LastName}"
})
.FirstOrDefaultAsync();
The latter would fetch only the requested data Instead of loading everything
enrico11011
enrico11011OP4w ago
ahh ok so that's how you do it. TIL thanks

Did you find this page helpful?