实体框架6编译LINQ查询

时间:2014-10-04 10:06:13

标签: c# linq entity-framework caching compiled-query

我试图通过缓存查询来提高Web应用程序的性能。

    public static Func<myEntity, List<HASHDuplicates>, IQueryable<FormResponse>> CompiledDuplicatedResponses =
    CompiledQuery.Compile<myEntity, List<HASHDuplicates>, IQueryable<FormResponse>>(
    (db, hashes) => from r in db.FormResponse
                    from h in db.IndexHASHes
                    from d in hashes
                    where r.id == h.FormResponseID && h.IndexHASHString == d.hash
                    select r);

我收到的错误是在编译时:

类型&#39; myEntity&#39;不能用作类型参数&#39; TArg0&#39;在泛型类型或方法中,System.Data.Entity.Core.Objects.CompiledQuery.Compile(System.Linq.Expressions.Expression&gt;)&#39;。来自&#39; myEntity&#39;没有隐式参考转换。 to&#39; System.Data.Entity.Core.Objects.ObjectContext&#39;。

我正在使用EF6

1 个答案:

答案 0 :(得分:19)

好吧,似乎在EF5及更高版本中,查询会自动编译,无需编译它们。 ObjectContext不再使用了,我们现在有了DbContext: Compiled Query no implicit reference conversion to ObjectContext

关于编译查询的另一篇有趣帖子: http://blog.codinghorror.com/compiled-or-bust/