EF Code First CTP5,使用Include方法与多对多表 [英] EF Code First CTP5, using Include method with Many to Many table

查看:463
本文介绍了EF Code First CTP5,使用Include方法与多对多表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有如下几个关系表格。

I have something like the following many to 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; }
}

,并获取商店信息。

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

我想做的是将一个Lambda表达式的Include方法称为许多关系而不是字符串。我试图实现如下代码:

What I want to do is call the Include method with a Lambda expression for a many to many relationship instead of string. I've tried to implement like the 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 guess I cannot compile it. Actually, the first code snippet works well, so basically it's okay, though, I'm kinda curious whether there is a work around or not.

任何帮助将不胜感激,

Yoo

推荐答案

b
$ b

Try this:

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

但您应该注意@Kristof在评论中建议的内容。

But you should follow what @Kristof suggested in comment.

这篇关于EF Code First CTP5,使用Include方法与多对多表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆