[英]How to include 2 navigational properties in EF?
An object in my database has 2 navigational properties (B and C): 我的数据库中的一个对象具有2个导航属性(B和C):
Object A
{
B bProperty
C cProperty
}
I wish to include them both when querying object A. I tried doing the following: 我希望在查询对象A时将它们都包括在内。我尝试执行以下操作:
dbcontext.A.Include(x => x.B).ToList();
But how do I include C too? 但是我如何也包含C?
Try this 尝试这个
dbcontext.A.Include(x => xB).Include(x => xC).ToList(); dbcontext.A.Include(x => xB).Include(x => xC).ToList();
I do it all in one go, so in my EF repository class, I have a method called GetAllIncluding which equals do it in a generic way for each entity, 我一口气做完了所有事情,因此在我的EF存储库类中,我有一个名为GetAllIninclude的方法,该方法等于以通用方式对每个实体进行操作,
public IQueryable<T> GetAllIncluding(params Expression<Func<T, object>>[] includes)
{
var query = DbSet.AsNoTracking();
query = includes.Aggregate(query, (current, includeProperty) => current.Include(includeProperty));
return query;
}
where DbSet is a private member of type IDbSet and T is a of type BaseEntity. 其中DbSet是IDbSet类型的私有成员,T是BaseEntity类型的成员。
and the way I use it is like this 我的使用方式就是这样
MyGenericRepository<A>().GetAllIncluding(x=> x.B, x=> x.C).FirstOrDefault();
Hope that helps. 希望能有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.