ExecuteStoreQuery with Dbcontext
Asked Answered
M

2

24

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?

Maleki answered 28/12, 2011 at 13:15 Comment(0)
D
38

A DbContext is simply a wrapper around the ObjectContext.

You can still access the original ObjectContext by using IObjectContextAdapter

(dbContext as IObjectContextAdapter).ObjectContext;
Deadwood answered 28/12, 2011 at 13:28 Comment(1)
I use this method successfully in my application.Lido
F
38

I want to add that I think now the correct method is:

dbContext.Database.SqlQuery<T>(string sql);
Fibrous answered 11/9, 2012 at 20:15 Comment(1)
If you want to track changes to the returned objects, you should consider dbContext.tablename.SqlQuery<T>(...) where tablename is your collection name. (i.e. a DbSet)Fluviatile

© 2022 - 2024 — McMap. All rights reserved.