本文介绍了实体框架6编译LINQ查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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



类型'myEntity'不能用作通用类型或方法'System.Data.Entity.Core.Objects.CompiledQuery.Compile(System.Linq.Expressions.Expression>)中的类型参数'TArg0' 。没有从'myEntity'到'System.Data.Entity.Core.Objects.ObjectContext'的隐式引用转换。



我使用的是EF6

解决方案

好的,在EF5和更大的查询中,查询被自动编译,没有必要编译它们。
不再使用ObjectContext,现在我们现在有DbContext:



另一个有趣的帖子在编译查询:


I'm trying to improve performance of a web application by caching a query.

    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);

The error I receive is at compile time:

The type 'myEntity' cannot be used as type parameter 'TArg0' in the generic type or method 'System.Data.Entity.Core.Objects.CompiledQuery.Compile(System.Linq.Expressions.Expression>)'. There is no implicit reference conversion from 'myEntity' to 'System.Data.Entity.Core.Objects.ObjectContext'.

I'm using EF6

解决方案

Ok it seems that in EF5 and greater the queries are automatically compiled and there is no need to compile them.The ObjectContext is not used anymore, and we have now DbContext:Compiled Query no implicit reference conversion to ObjectContext

Another interesting post on Compiled Query:http://blog.codinghorror.com/compiled-or-bust/

这篇关于实体框架6编译LINQ查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 14:56