Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework 6 Compiled LINQ Query

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

like image 664
Giox Avatar asked Oct 04 '14 10:10

Giox


People also ask

What is compiled query in LINQ?

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.

Can we use LINQ with Entity Framework?

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.

Does Entity Framework cache data?

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.


1 Answers

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/

like image 80
Giox Avatar answered Sep 20 '22 19:09

Giox