[英]Entity framework make compiled query generic
I use entity framework and Im trying to make such a query generic, so it can be used for any entity type (with an assumption that each entity has property int Id
). 我使用实体框架,我试图使这样的查询通用,因此它可以用于任何实体类型(假设每个实体都具有属性
int Id
)。
I tried something like this, but there is no collection ctx.TEntity or something similiar: 我试过这样的东西,但没有收集ctx.TEntity或类似的东西:
public class Queries<TEntity> where TEntity : AbstractEntity
{
public Func<AdventureWorksEntities, int, TEntity> getQuery() {
return
CompiledQuery.Compile<AdventureWorksEntities, int, Entity>(
(ctx, num) => ctx.TEntity.First(x => x.Id>num));
}
}
AbstractEnitity: AbstractEnitity:
public abstract class AbstractEntity {
[Key]
public int Id {get; set};
}
Thanks for your ideas :) 谢谢你的想法:)
I tried Compiled Queries with a DbContext, with no success (it is not supported, and the workaround were not suitable for me too). 我尝试使用DbContext编译查询,但没有成功(它不受支持,并且解决方法也不适合我)。 Have you checed this link : http://blogs.msdn.com/b/efdesign/archive/2011/06/30/auto-compiled-linq-queries-entity-framework-june-2011-ctp.aspx ?
你有没有打过这个链接: http : //blogs.msdn.com/b/efdesign/archive/2011/06/30/auto-compiled-linq-queries-entity-framework-june-2011-ctp.aspx ?
You can see this article : http://social.msdn.microsoft.com/Forums/en-US/0c07e1d6-7db6-4348-b106-e576d3153b70/ef-40-compiled-queries-performance?forum=adonetefx . 您可以看到这篇文章: http : //social.msdn.microsoft.com/Forums/en-US/0c07e1d6-7db6-4348-b106-e576d3153b70/ef-40-compiled-queries-performance?forum=adonetefx 。 I don't think using Compiled Queires make a big difference espacially with modern versions of EF (5 & 6).
我不认为使用Compiled Queires会与现代版本的EF(5和6)产生很大的不同。
On the first query, the connection is set up, and it can take some time. 在第一个查询中,连接已设置,可能需要一些时间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.