Sorry the title isn't more specific - I didn't know how to describe this succinctly. I have Trips and Location that have a many-to-many relationship - straightforward except that Locations have no need to know about the Trips that use them. I've created these entities to represent this:
public class Trip
{
public int TripId { get; set; }
public virtual IList<TripLocation> TripLocations { get; set; }
}
public class TripLocation
{
public int TripId { get; set; }
public int LocationId { get; set; }
public virtual Location Location { get; set; }
}
public class Location
{
public int LocationId { get; set; }
// Note: Intentionally no collection of Trips
}
I can get the Trip to eager load it's TripLocations but I can't get the TripLocations to eager load their Locations. I've tried a bunch of combinations fluent configuration and "Include"ing in the query such as
IQueryable<Trip> query = from trip in context
.Include(r =>r.TripLocations)
.Include(r => r.TripLocations.Select(tl => tl.Location))
select ride;
Any suggestions much appreciated!
TripLocationentity in your model? Are there other properties in this entity which you are not showing? Otherwise you can remove this entity from your model altogether and create a many-to-many relationship directly betweenTripandLocation. In your current model you actually have two one-to-many and not a many-to-many relationship.modelBuilder.Entity<Trip>().HasMany(t => t.Locations).WithMany().Map(...);. UsingWithMany()without parameter is for the case when you don't have aTripscollection inLocation.TripLocation). If you have such additional properties you can't create a many-to-many relation at all, you must create two one-to-many relationship and expose the join table as an entity in the model: stackoverflow.com/a/7053393/270591