© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
7 replies
linqisnice

❔ accessing collections via principal entity vs directly on dbset

what is generally preferred if i want the accommodations of a listing (WITH resource ownership check included, so firstasync is intentional here)? s

Doing this:
var listing = await _dbContext.Listings
     .AsSplitQuery()
     .Include(x => x.Accommodations).ThenInclude(x => x.Inventory)
     .FirstAsync(x => x.Id == request.ListingId && x.Host.UserId == currentUserId);

      var accommodations = listing.Accommodations... // just an example
var listing = await _dbContext.Listings
     .AsSplitQuery()
     .Include(x => x.Accommodations).ThenInclude(x => x.Inventory)
     .FirstAsync(x => x.Id == request.ListingId && x.Host.UserId == currentUserId);

      var accommodations = listing.Accommodations... // just an example

or actually querying accommodations directly?

I find that the above seems better, since it will immediately return the request if the user does not have access. whereas if we query accommodations, it's going to reutnr an empty list and then continue


I guess my question pertains in general to resource ownership and accessing a collection items via prinicipal entity vs directly on the dbset.

My strategy is generally to use firstasync since if the user is not the owner of the listing, they have likely tampered with the inspector or some such to finagle the resource
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Collections in Entity Framework
C#CC# / help
4y ago
Does DbSet.FirstOrDefaultAsync return a tracked entity?
C#CC# / help
4y ago
✅ .NET 8, EF Core, Fluent API - Schema definition is ignored when accessing via DbSet
C#CC# / help
2y ago
❔ DBSet manipulation
C#CC# / help
3y ago