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

c# entity-framework sql

Question

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())
{    
}
1
3
8/3/2012 8:25:23 AM

Popular Answer

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

4
8/3/2012 8:43:39 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow