How to use DbContext.Database.SqlQuerylt;TElementgt;(sql, params) with stored procedure? EF Code First CTP5(如何将 DbContext.Database.SqlQuerylt;TElementgt;(sql, params) 与存储过程一起使用?EF 代码优先 CTP5)
问题描述
我有一个包含三个参数的存储过程,我一直在尝试使用以下方法返回结果:
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<myEntityType>("mySpName", param1, param2, param3);
起初我尝试使用 SqlParameter 对象作为参数,但这不起作用并抛出了 SqlException 并显示以下消息:
At first I tried using SqlParameter objects as the params but this didn't work and threw a SqlException with the following message:
过程或函数mySpName"需要参数@param1",但未提供.
Procedure or function 'mySpName' expects parameter '@param1', which was not supplied.
所以我的问题是如何将此方法与需要参数的存储过程一起使用?
So my question is how you can use this method with a stored procedure that expects parameters?
谢谢.
推荐答案
您应该通过以下方式提供 SqlParameter 实例:
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)
);
这篇关于如何将 DbContext.Database.SqlQuery<TElement>(sql, params) 与存储过程一起使用?EF 代码优先 CTP5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何将 DbContext.Database.SqlQuery<TElement>(sql, params) 与存储过程一起使用?EF 代码优先 CTP5
基础教程推荐
- C# 9 新特性——record的相关总结 2023-04-03
- SonarQube C# 分析失败“不是指针的有效行偏移" 2022-01-01
- 重新排序 WPF TabControl 中的选项卡 2022-01-01
- 在 VB6 或经典 ASP 中使用 .NET 2022-01-01
- 将数据集转换为列表 2022-01-01
- Mono https webrequest 失败并显示“身份验证或解密失败" 2022-01-01
- 获取C#保存对话框的文件路径 2022-01-01
- 从 C# 控制相机设备 2022-01-01
- 更新 Visual Studio 中的 DataSet 结构以匹配新的 SQL 数据库结构 2022-01-01
- 如果条件可以为空 2022-01-01
