如何将 DbContext.Database.SqlQuery<TElement>(sql, params) 与存储过程一起使用?EF 代码优先 CTP5 [英] How to use DbContext.Database.SqlQuery<TElement>(sql, params) with stored procedure? EF Code First CTP5

查看:20
本文介绍了如何将 DbContext.Database.SqlQuery<TElement>(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&lt;TElement&gt;(sql, params) 与存储过程一起使用?EF 代码优先 CTP5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆