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
基础教程推荐
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
