[英]DbContext and Generics
With DataContext I could do something like 使用DataContext,我可以做类似的事情
public IQueryable<T> All()
{
return db.CreateObjectSet<T>().AsQueryable();
}
(as part of a generic repository class - the rest of the CRUD actions are handled in a similar manner) (作为通用存储库类的一部分-其余的CRUD操作以类似的方式处理)
I'm looking to see if the same is possible with DbContext : ie can a return a Queryable of T? 我正在寻找DbContext是否可能实现相同的功能:即可以返回T的Queryable吗? Entry looks a bit like it might help, but not quite... ideas anyone ? 进入看起来有点像它可能会有所帮助,但并不是完全...有人吗?
DbSet
implements IQueryable
DbSet
实现IQueryable
this should work: 这应该工作:
public IQueryable All()
{
return db.Set<T>();
}
If you wish to return a Queryable would be something like: 如果您希望返回Queryable,则将类似于:
public virtual IQueryable GetAll()
{
return dbContext.Set<T>();
}
In fact, I blogged some time ago about how to create a generic repository using EF4.1 take a look: 实际上,我前段时间写了一篇关于如何使用EF4.1创建通用存储库的博客:
http://davidandersonlino.net/blog/2011/04/28/generic-repository-pattern-com-entity-framework-4-1/ http://davidandersonlino.net/blog/2011/04/28/generic-repository-pattern-com-entity-framework-4-1/
Hope I answered your question. 希望我回答了你的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.