简体   繁体   English

EF6 存储过程不接受参数

[英]EF6 Stored Procedure does not accept parameters

I am using EF6 and I need to execute stored procedure.我正在使用 EF6,我需要执行存储过程。 It takes two parameters:它需要两个参数:

  • mIIN varchar(12) , which is input string mIIN varchar(12) ,即输入字符串
  • mXMLOutput varchar(max) , which is the result of execution mXMLOutput varchar(max) ,这是执行的结果

It works fine when I call it from Management Studio.当我从 Management Studio 调用它时,它工作正常。

However, when I use the following code:但是,当我使用以下代码时:

var iinParameter = new SqlParameter("@mIIN", SqlDbType.VarChar, 12) { Value = "123456123456" };
var outputParameter = new SqlParameter("@mXMLContent", SqlDbType.VarChar, -1) { Direction = ParameterDirection.Output };
_dbContext.Database.ExecuteSqlCommand("GetInfo", iinParameter, outputParameter);

which results in the error导致错误

Procedure or function 'GetInfo' expects parameter '@mIIN', which was not supplied过程或 function 'GetInfo' 需要参数 '@mIIN',但未提供

EF6 contains the following messages: EF6 包含以下消息:

Started transaction at 15.12.2015 14:27:27 +06:00  
GetInfo  
-- @mIIN: '123456123456' (Type = AnsiString, IsNullable = false, Size = 12)   
-- @mXMLContent: '' (Type = AnsiString, Direction = Output, IsNullable = false, Size = -1)  
-- Executing at 15.12.2015 14:27:27 +06:00  
-- Failed in 2 ms with error: Procedure or function 'GetInfo' expects parameter '@mIIN', which was not supplied.

What can cause that problem?什么会导致这个问题? I am passing this parameter, why doesn't it accept it?我正在传递这个参数,为什么它不接受它?

这应该工作:

_dbContext.Database.ExecuteSqlCommand("exec GetInfo @mIIN, @mXMLContent OUTPUT", iinParameter, outputParameter);

I think that problem is in your subprocedure. 我认为问题出在您的子过程中。 Log message tells that parameter was suplied to GetInfo , and error is related to subprocedure IBRC_GetClientInfo . 日志消息表明该参数已提供给GetInfo ,并且错误与子过程IBRC_GetClientInfo Check your procedure that parameter inside is properly passed. 检查您的过程,以确保正确传递了内部参数。

There are few things that are must to call a store procedure using EF6.使用 EF6 调用存储过程需要做的事情很少。

  1. Provide all parameters including optional parameters.提供所有参数,包括可选参数。
  2. Define parameter values as SqlParameter.将参数值定义为 SqlParameter。
  3. Provide all values as string in SqlParameter constructor.在 SqlParameter 构造函数中提供所有值作为字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM