EF Code First CTP5 using the Include method with a Many to Many table

I have something like the following many, many relationship tables.

public class Shop{ public int Id { get; set; } public virtual ICollection<ShopFacility> ShopFacilities { get; set; } } public class ShopFacility { public int Id { get; set; } public int ShopId { get; set; } public int FacilityId { get; set; } public virtual Shop Shop { get; set; } public virtual Facility Facility { get; set; } } public class Facility { public int Id { get; set; } public virtual ICollection<ShopFacility> ShopFacilities { get; set; } } 

and get information about stores.

 using (var context = new DataContext()) { return context.Shops.Include(s => s.ShopFacilities) .Include("ShopFacilities.Facility") // This line .First(x => x.Id == id); } 

What I want to do is call the Include method with a Lambda expression for a many-many relationship instead of a string. I tried to implement as below code:

 using (var context = new DataContext()) { return context.Shops.Include(s => s.ShopFacilities) .Include(s => s.ShopFacilities.Facility) // Cannot compile .First(x => x.Id == id); } 

But, as you suppose, I can not compile it. Actually, the first code snippet works well, so basically it's good, however, I'm curious if there is work or not.

Any help would be appreciated

YU

+6
linq-to-entities entity-framework ef-code-first
Mar 09 2018-11-11T00:
source share
1 answer

Try the following:

 return context.Shops.Include(s => s.ShopFacilities.Select(f => f.Facility)) .First(x => x.Id == id); 

But you should follow what @Kristof suggested in the comment.

+16
Mar 09 2018-11-11T00:
source share



All Articles