2014-10-28 49 views
1

在我的数据库中的对象有2种导航性能(B和C):如何在EF中包含2个导航属性?

Object A 
{ 
    B bProperty 

    C cProperty 
} 

我希望包括他们俩当查询对象A 我试着做以下几点:

dbcontext.A.Include(x => x.B).ToList(); 

但我怎么也包括C呢?

回答

3

尝试此

dbcontext.A.Include(X => x.B).INCLUDE(X => x.C).ToList();

我把一切都一气呵成,所以在我的EF库类,我有一个名为GetAllIncluding方法相当于做它在每个实体的通用方式,

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; 
} 

其中DbSet是一个私有成员类型IDbSet,T是BaseEntity类型。

我用它的方式是这样的

MyGenericRepository<A>().GetAllIncluding(x=> x.B, x=> x.C).FirstOrDefault(); 

希望有所帮助。