Entity Framework - eager loading of related entities

前端 未结 3 912
無奈伤痛
無奈伤痛 2021-02-14 21:44

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 t

3条回答
  •  孤街浪徒
    2021-02-14 22:22

    Regarding the lambda expression, you can use context.Set like @tyron said or you can use context.Trips. For example:

    IQueryable query = from ride in context.Trips
                                 .Include(t=>t.TripLocations.Select(l=>l.Location))                                     
                             select ride;
    

    In order to make this code work, you need to define a property of type DbSet in your DbContext class like below:

    public DbSet Trips { get; set; }
    

    Defining a property that returns DbSet is nice but at the same time it's equivalent to accesing context.Set. It's just a code style that could also be combined.

提交回复
热议问题