C
C#2w ago
jadders

Why isn't the blogposts tags being pulled?

So the first screenshot is of the index page, where I'm trying to display the tags. The second, third and fourth screenshots are of the Blogpost, Tag, and BlogpostTag models The next 3 images are of each of the databases, in the same order. finally, here is a screenshot of the webapp running. Why isn't it finding the tags when they exist in the DB?
No description
No description
No description
No description
No description
No description
No description
No description
30 Replies
Anton
Anton2w ago
you didn't include the code that runs the query I assume you have an EF Core query you need to either project into a dto, or do an Include for the tags navigation and remove the check for null, do an assert instead
Angius
Angius2w ago
gib query
jadders
jaddersOP2w ago
sure, 2 secs
jadders
jaddersOP2w ago
No description
jadders
jaddersOP2w ago
BlogpostsController
Angius
Angius2w ago
Huh, seems fine Does the debugger also show that blogpost here doesn't contain tags? As a side note, you can just set the relationship between tags and blogposts directly, EF will figure out the pivot table
jadders
jaddersOP2w ago
@Angius How would I set it directly, just like the new line I added?
No description
Angius
Angius2w ago
ye Just List<Tag> Tags on the blogpost, and List<Blogpost> Blogposts on the tag
jadders
jaddersOP2w ago
Ahh, the guide I was using said to make a connector class?
Angius
Angius2w ago
Wouldn't make them nullable either
jadders
jaddersOP2w ago
That's basically an interface right, what my old code did?
Angius
Angius2w ago
Well, the guide probably wasn't the freshest Your code does not contain any interfaces whatsoever Besides IActionResult I guess
jadders
jaddersOP2w ago
ahh, so the connector class is pointless?
Angius
Angius2w ago
It has its uses But you can always configure the relationship to use it While keeping the navigation between tags and posts direct
jadders
jaddersOP2w ago
No description
Angius
Angius2w ago
Example from my project, configuration of Story
builder
.HasMany(s => s.Tags)
.WithMany(t => t.Stories)
.UsingEntity<StoryTag>(
st => st.HasOne(e => e.Tag)
.WithMany()
.HasForeignKey(e => e.TagId)
.OnDelete(DeleteBehavior.Cascade),
st => st.HasOne(e => e.Story)
.WithMany()
.HasForeignKey(e => e.StoryId)
.OnDelete(DeleteBehavior.Cascade)
builder
.HasMany(s => s.Tags)
.WithMany(t => t.Stories)
.UsingEntity<StoryTag>(
st => st.HasOne(e => e.Tag)
.WithMany()
.HasForeignKey(e => e.TagId)
.OnDelete(DeleteBehavior.Cascade),
st => st.HasOne(e => e.Story)
.WithMany()
.HasForeignKey(e => e.StoryId)
.OnDelete(DeleteBehavior.Cascade)
jadders
jaddersOP2w ago
ohhhh
Angius
Angius2w ago
Just set it to [] public List<Tag> Tags { get; set; } = [];
jadders
jaddersOP2w ago
No description
jadders
jaddersOP2w ago
so do I just change this to now just: .Include(t => t.Tag) .FirstOrDefaultAsync(x => x.Id == id); as the Include doesn't need to include the blogpostTags @Angius it broke my context
Angius
Angius2w ago
Yes, that's how you do it, just include the tags
jadders
jaddersOP2w ago
No description
Angius
Angius2w ago
Broke the context how? Don't configure the BlogpostTags
jadders
jaddersOP2w ago
No description
jadders
jaddersOP2w ago
Every change I make is wrong xD Any chance you have 2 mins for a voice call to run through it quick and I'll share my screen??
Angius
Angius2w ago
Not right now, sorry
jadders
jaddersOP2w ago
Okay, no worries
Unknown User
Unknown User2w ago
Message Not Public
Sign In & Join Server To View
jadders
jaddersOP2w ago
I should have just posted the repo link xD Just need to check my gitignore worked properly will post it shortly Worked this out. I hadn't passed the BlogpostTags into the Index method in the controller before returning the View

Did you find this page helpful?