1
我在.net4.0中使用EF5.0,并且我有三个像这样的多对多相关连接的实体:用ef代码优先查询三个(或多个)多对多相关实体
public class A
{
public int Id { get; set; }
public ICollection<B> Bs { get; set; }
}
public class B
{
public int Id { get; set; }
public ICollection<A> As { get; set; }
public ICollection<C> Cs { get; set; }
}
public class C
{
public int Id { get; set; }
public ICollection<B> Bs { get; set; }
}
,这是我的映射代码:
HasMany(a => a.Bs).WithMany(b => b.As).Map(m =>
{
m.ToTable("A_B");
});
HasMany(c => c.Bs).WithMany(b => b.Cs).Map(m =>
{
m.ToTable("B_C");
});
对于有一定情况下,我怎么可能让所有的B间接相关的铯?
我希望得到一个干净的SQL查询,如:
select C.* from C
join B_C on B_C.Cid = C.id
join A_B on A_B.Bid = B_C.Bid
join A on A.id = A_B.Aid
where A.id = 1
折轴我得到的SQL没有给定结表中的实体类型? 我下面的代码有一个很丑的SQL与嵌套查询:
var res = Context.C.Where(
c => c.Bs.SelectMany(b => b.As.Select(a => a.Id))
.Contains(theA.Id)
);
请帮助我,谢谢了很多!
感谢您的回复,但此查询使SQL比我更复杂......它会产生一个更嵌套查询和多了一个“存在”。 – akara