18

I am encountering some performance problems with my Entity Framework Code-First queries and I believe that precompilation may be the answer. If I were using "normal" Entity Framework, I would simply use the CompiledQuery.Compile method to precomiple my queries. But since I have a DbContext and not an ObjectContext, I can't get this to work.

I do realize that DbContext is an IObjectContextAdapter, which gives me access to the ObjectContext, but I cannot find the method that lets me get an IQueryable from my object context that works in my precompiled query. I tried to use CreateObjectSet, but when EF tried to run the query it complained that it couldn't convert that method into SQL.

So what is the best way to precompile LINQ queries against a Code-First DbContext?

John Bledsoe
  • 17,142
  • 5
  • 42
  • 59

2 Answers2

11

This will be probably solved in EFv4.2 EF vNext (currently in the very first CTP) by auto-compiled LINQ queries.

Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
  • This means that it will only be available on .NET 4.5? Or will it be released in EF4.3? – Nelson Reis Oct 24 '11 at 11:11
  • 2
    @billy: No. At the moment it is only available with .NET 4.5 but you can use [this](http://blog.oneunicorn.com/2012/10/05/ef-enums-and-spatial-types-on-net-4/) to have it in .NET 4.0. – Ladislav Mrnka Oct 28 '12 at 18:32
9

As from the official announecment:

"No compiled query support from DbContext Unfortunately due to some technical limitations in the compiled query functionality we shipped in .NET Framework 4.0 we are unable to support compiled queries via the DbContext API. We realize this is a painful limitation and will work to enable this for the next release. "

Link.

BennyM
  • 2,796
  • 16
  • 24