How to use DbContext.Database.SqlQuery<TElement>(sql, params) with stored procedure? EF Code First CTP5
You should supply the SqlParameter instances in the following way:
context.Database.SqlQuery<myEntityType>( "mySpName @param1, @param2, @param3", new SqlParameter("param1", param1), new SqlParameter("param2", param2), new SqlParameter("param3", param3));
Also, you can use the "sql" parameter as a format specifier:
context.Database.SqlQuery<MyEntityType>("mySpName @param1 = {0}", param1)
This solution is (only) for SQL Server 2005
You guys are lifesavers, but as @Dan Mork said, you need to add EXEC to the mix. What was tripping me up was:
- 'EXEC ' before the Proc Name
- Commas in between Params
- Chopping off '@' on the ParamDefinitions (not sure that bit is required though).
:
context.Database.SqlQuery<EntityType>( "EXEC ProcName @param1, @param2", new SqlParameter("param1", param1), new SqlParameter("param2", param2));