EF包括其他实体(通用存储库模式)。
|
我在Entity Framework Code First之上使用通用存储库模式。一切工作正常,直到我需要在查询中包含更多实体。我必须成功包含一个实体,但是现在我不知道如何包含多个实体。看看到目前为止我有什么:
public IQueryable<TEntity> GetQuery<TEntity>() where TEntity : class
{
var entityName = GetEntityName<TEntity>();
return _objectContext.CreateQuery<TEntity>(entityName);
}
public IList<TEntity> GetQueryWithInclude<TEntity>(string toInclude) where TEntity : class
{
var entityName = GetEntityName<TEntity>();
return _objectContext.CreateQuery<TEntity>(entityName).Include(toInclude).ToList();
}
private string GetEntityName<TEntity>() where TEntity : class
{
return string.Format(\"{0}.{1}\", _objectContext.DefaultContainerName, _pluralizer.Pluralize(typeof(TEntity).Name));
}
我试着做但没用的是将字符串数组传递给函数,然后尝试“附加”查询中的包含内容。我想知道是否一次调用GetQueryWithInclude并传递一个实体名称(实际上是一个导航属性)来聚合查询结果,但是我担心这可能会在每次调用时重复查询结果。您认为什么是使它起作用的最佳方法?
提前致谢!
更新:
这是我要达到的目标的一个示例:
public IQueryable GetQueryWithIncludes(string[] otherEntities)
{
var entityName = GetEntityName<TEntity>();
//now loop over the otherEntities array
//and append Include extensions to the query
//so inside the loop, something like:
_objectContext.GetQuery<TEntity>(entityName).Include(otherEntities[index]);
}
没有找到相关结果
已邀请:
3 个回复
泪琉踞檄
您将使用它作为示例,例如:
该查询将向所有客户加载他们的地址和订单,并且每个订单都将包含其订单项。
坝镰补翔奋
破弯