How to use DbContext.Database.SqlQuery(sql, params) with stored procedure? EF Code First CTP5

后端 未结 10 1926
梦谈多话
梦谈多话 2020-11-22 16:53

I have a stored procedure that has three parameters and I\'ve been trying to use the following to return the results:

context.Database.SqlQuery

        
10条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-22 17:50

    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 Param Definitions (not sure that bit is required though).

    :

    context.Database.SqlQuery(
        "EXEC ProcName @param1, @param2", 
        new SqlParameter("param1", param1), 
        new SqlParameter("param2", param2)
    );
    

提交回复
热议问题