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
基础教程推荐
- .NET SerialPort DataReceived 事件未触发 2022-01-01
- 如果有人提交恶意软件Nuget包怎么办? 2022-01-01
- 如何使用 .Net 检查 Active Directory 服务器是否已启动并正在运行? 2022-01-01
- C# 从 List<List<int>> 中删除重 2022-01-01
- 当值可以是对象或空数组时反序列化 JSON 2022-01-01
- Moq It.Is<>不匹配 2022-01-01
- WPF 模态进度窗口 2022-01-01
- 禁止输入少量字符,例如'<'、'&a 2022-01-01
- Azure Functions:CosmosDBTrigger 未在 Visual Studio 中触发 2022-01-01
- 我应该在后面的代码中直接使用 Linq To SQL 还是使 2022-01-01
