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


- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- C# 中多线程网络服务器的模式 2022-01-01
- 输入按键事件处理程序 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01