如何使用DbContext.Database.SqlQuery&LT; TElement&GT;(SQL,则params)与存储过程? EF code首先CTP5 [英] How to use DbContext.Database.SqlQuery<TElement>(sql, params) with stored procedure? EF Code First 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
对象为PARAMS但这并没有工作,并投掷了 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'需要参数'@参数1',但未提供。
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&LT; TElement&GT;(SQL,则params)与存储过程? EF code首先CTP5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!