title: How to wrap Entity Framework to intercept the LINQ expression just before execution?
body: <p>I want to rewrite certain parts of the LINQ expression just before execution. And I'm having problems injecting my rewriter in the correct place (at all actually).</p>
  
  <p>Looking at the Entity Framework source (in reflector) it in the end comes down to the <code>IQueryProvider.Execute</code> which in EF is coupled to the expression by the <code>ObjectContext</code> offering the <code>internal IQueryProvider Provider { get; }</code> property.</p>
  
  <p>So I created a a wrapper class (implementing <code>IQueryProvider</code>) to do the Expression rewriting when the Execute gets called and then pass it to the original Provider. </p>
  
  <p>Problem is, the field behind <code>Provider</code> is <code>private ObjectQueryProvider _queryProvider;</code>. This <code>ObjectQueryProvider</code>  is an <strong>internal sealed class</strong>, meaning it's not possible to create a subclass offering the added rewriting. </p>
  
  <p>So this approach got me to a dead end due to the very tightly coupled ObjectContext.</p>
  
  <p>How to solve this problem? Am I looking in the wrong direction? Is there perhaps a way to inject myself around this <code>ObjectQueryProvider</code>?</p>
  
  <p><strong>Update</strong>: While the provided solutions all work when you're "wrapping" the ObjectContext using the Repository pattern, a solution which would allow for direct usage of the generated subclass from ObjectContext would be preferable. Hereby remaining compatible with the Dynamic Data scaffolding. </p>