I’m working with ASP.NET Web API and need help with returning collections of related entities. I have two models: Event and PhotoLink. Each Event can have multiple PhotoLink records associated with it through a foreign key relationship.
Here’s my current controller method that works for single related objects:
[Route("~/api/events/{eventId:int}")]
public async Task<IHttpActionResult> GetEventById(int eventId)
{
var eventData = await db.Events.Include(e => e.Category)
.Where(e => e.EventId == eventId)
.Select(MapToEventDto)
.FirstOrDefaultAsync();
if (eventData == null)
return NotFound();
return Ok(eventData);
}
But I can’t figure out how to include all the PhotoLink objects for each Event. I want my API response to include the event details plus an array of all associated photos. What’s the proper way to structure the Include statement and DTO mapping to achieve this? Any guidance would be really helpful.
Yeah, everyone’s giving solid EF advice, but you’ll hit scaling problems fast. Those Include statements create messy queries that slow down as your data grows.
I had this exact issue at my company. Events with tons of photos were killing our response times. Every data structure change meant updating controllers and DTOs.
We fixed it with an automated workflow. When an event gets created or updated, the system automatically grabs related photos and structures the response for the frontend. No more complex EF queries or mapping headaches.
The workflow handles relationships, caches popular events with photos, and optimizes image sizes based on display location. Your API just returns clean, pre-structured data.
Best part - you can set different response formats for different clients without touching controller code. Mobile gets thumbnails, web gets full resolution, admin gets metadata. All automated based on request source.
Here’s what I learned: EF Core handles collection includes differently than single properties. Your Event model needs a proper navigation property like public virtual ICollection<PhotoLink> PhotoLinks { get; set; }. Without it, Include won’t work.
Also check for N+1 query problems. I added .AsSplitQuery() after my includes to avoid cartesian explosion with multiple collections. Performance got way better.
You need to load the PhotoLinks upfront but skip the Select mapping in the query. Here’s how I handle it:
var eventData = await db.Events
.Include(e => e.Category)
.Include(e => e.PhotoLinks)
.FirstOrDefaultAsync(e => e.EventId == eventId);
if (eventData == null)
return NotFound();
var dto = MapToEventDto(eventData);
return Ok(dto);
Do the Select mapping after you get the entity, not during the query. When you mix Select with Include, EF gets confused about what to actually include.
I’ve seen this trip up people constantly. Include works fine but the Select projection runs before EF can load the related data properly.
Check that your MapToEventDto method handles the PhotoLinks collection correctly. If it’s not mapping the collection, that’s a different problem with your mapping logic.
just add .Include(e => e.PhotoLinks) in ur query chain. like db.Events.Include(e => e.Category).Include(e => e.PhotoLinks). and make sure to map the photolinks in ur DTO too. should work fine with EF’s lazy loading.
The problem is your DTO mapping isn’t handling the collection correctly. When you chain db.Events.Include(e => e.Category).Include(e => e.PhotoLinks), EF loads everything in one query - that part’s fine. But your MapToEventDto method needs fixing. Make sure your EventDto has public List<PhotoLinkDto> PhotoLinks { get; set; } and your mapping function converts each PhotoLink entity to its DTO. I’ve run into issues using Select with anonymous objects on collections - if you’re using AutoMapper, try ProjectTo instead, or just handle the mapping after the database call. Also double-check that your navigation properties have the right foreign key relationships set up.