ExecuteStoreQuery with Dbcontext

Chuck Norris picture Chuck Norris · Dec 28, 2011 · Viewed 18.2k times · Source

I want to use ExecuteStoreQuery function of Entity Framework and I was wondered that my context variable didn't have ExecuteStoreQuery method.

So, I discovered that it's a method of ObjectContext class, but I've already used DbContext for my application. I simply had tried to change DbContext with ObjectContext, but it brought some errors(for example, in ObjectContext isn't OnModelCreating method).

How can I use ExecuteStoreQuery with DbContext and if I can't, is any alternatives of ExecuteStoreQuery in DbContext?

Answer

Wouter de Kort picture Wouter de Kort · Dec 28, 2011

A DbContext is simply a wrapper around the ObjectContext.

You can still access the original ObjectContext by using IObjectContextAdapter

(dbContext as IObjectContextAdapter).ObjectContext;