Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access context.Database.SqlQuery in Entity Framework?

I am trying to follow this tutorial:

http://blogs.msdn.com/b/diego/archive/2012/01/10/how-to-execute-stored-procedures-sqlquery-in-the-dbcontext-api.aspx

but can't seem to to access this method, it will not show up in intelli-sense

context.Database.SqlQuery

I am using the following code but can't seem to access the SqlQuery method:

using(Entities db = new Entities())
{    
}
like image 717
Funky Avatar asked Jan 27 '26 18:01

Funky


1 Answers

The method you have described is valid for executing SQL vs a DbContext version of EF. (DbContext is used for Code First and is also available for model first but you need to do a little setup). However in your example it should be the following.

using(Entities db = new Entities())
{   
    db.Database.SqlQuery(....);
}

If you are using OOB model first (ie edmx), you are probably using an ObjectContext, in which case you will need to perform the following:

using(Entities db = new Entities())
{   
    db.ExecuteStoreQuery<ReturnType>("...");
}

See: http://blogs.microsoft.co.il/blogs/gilf/archive/2009/11/25/execute-t-sql-statements-in-entity-framework-4.aspx

like image 61
Not loved Avatar answered Jan 30 '26 07:01

Not loved



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!