DbContext.Database.SqlQuery
To successfully execute a stored procedure with parameters using this method, the parameters should be provided as SqlParameter instances. The correct syntax is as follows:
<code class="language-csharp">context.Database.SqlQuery<我的实体类型>( "我的存储过程名称 @param1, @param2, @param3", new SqlParameter("param1", param1), new SqlParameter("param2", param2), new SqlParameter("param3", param3) );</code>
In this modified code, the sql parameters contain parameter names prefixed with "@". new SqlParameter
The subsequent parameters of the instance specify the parameter name to be replaced and the value of that parameter.
With this approach, the expected parameters of the stored procedure can be provided, allowing the query to be successfully executed and the results returned as an object of the specified type.
The above is the detailed content of How to Correctly Execute Stored Procedures with Parameters using DbContext.Database.SqlQuery?. For more information, please follow other related articles on the PHP Chinese website!