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
The CompiledQuery class provides compilation and caching of queries for reuse. Conceptually, this class contains a CompiledQuery's Compile method with several overloads. Call the Compile method to create a new delegate to represent the compiled query.
One of the most important features of Entity Framework is the LINQ to Entities query dialect. LINQ to Entities is a specialized version of LINQ that operates on Entity Framework models.
Entity Framework has the following forms of caching built-in: Object caching – the ObjectStateManager built into an ObjectContext instance keeps track in memory of the objects that have been retrieved using that instance. This is also known as first-level cache.
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With