EF 包括其他实体(通用存储库模式)
我在实体框架代码优先之上使用通用存储库模式。一切都工作正常,直到我需要在查询中包含更多实体。我必须成功包含一个实体,但现在我不知道如何包含多个实体。看看我到目前为止所得到的:
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]);
}
I am using the Generic Repository pattern on top of Entity Framework Code First. Everything was working fine until I needed to include more entities in a query. I got to include one entity successfully, but now I can't figure out how to include multiple entities. Check out what I've got so far:
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));
}
What I tried to do but didn't work was pass in an array of strings into a function, then try to "append" the includes on top of the query. I was wondering what if I called the GetQueryWithInclude and passed an entity name (actually a navigation property) at a time to aggregate the results of the query, but I'm worried this might duplicate the results of the query on each call... What do you think would be the best way to get this to work?
UPDATE:
Here's an example of what I'm trying to achieve:
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]);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
仅使用 IQueryable 上的 Include 扩展。它在 EF 4.1 组件中可用。如果您不想在上层中引用该程序集,请在数据访问程序集中创建包装器扩展方法。
这里有示例:
您将使用它,例如:
此查询将加载所有客户的地址和订单,并且每个订单将包含其订单项目。
Use just the Include extension on IQueryable. It is available in EF 4.1 assembly. If you don't want to reference that assembly in your upper layers create wrapper extension method in your data access assembly.
Here you have example:
You will use it for example like:
This query will load all customers with their addresses and orders and every order will contain its order items.
告别硬编码的 ObjectQuery(T)。包含调用
如果您使用 EF > 4、然后就内置了,查看MSDN上的 DbExtensions.Include 。
Say goodbye to the hardcoded ObjectQuery(T).Include calls
If you're using EF > 4, then it's built in, check DbExtensions.Include on MSDN.
//我在这里已经包含了最低限度的内容。下面介绍如何使用它。
//I have included the bare minimum here. Below is how to use it.
对于实体框架,创建一个字符串列表来保存包含内容。请参阅下面的示例代码
有关更多详细信息,请参阅下面的链接
http://bulletproofcoder.com/blog/using- include-in-a-generic-entity-framework-repository
在实体框架核心中,我们将使用IIncludableQueryable。请参阅下面的示例代码
For entity framework create a list of strings to hold the includes. See the below example code
For more detail see the below link
http://bulletproofcoder.com/blog/using-include-in-a-generic-entity-framework-repository
In entity framework core we shall use IIncludableQueryable. See the below example code