简体   繁体   English

无法在实体框架的DbCommand中传递参数

[英]Cannot Pass Parameter in DbCommand in Entity Framework

I have error when I try to pass parameter to stored procedure using DbCommand 尝试使用DbCommand将参数传递给存储过程时出现错误

Error returned: 返回错误:

Procedure or function 'Procedure_Name' expects parameter '@TEID', which was not supplied. 过程或函数“ Procedure_Name”需要未提供的参数“ @TEID”。

These are my procedure parameters: 这些是我的过程参数:

@PageNumber INT = 1,
@PageSize INT = 50,
@StartTime nvarchar(max) = -1 ,
@EndTime nvarchar(max) = -1 ,
@Month NVARCHAR(2) = -1,
@Year NVARCHAR(4) = -1,
@Day NVARCHAR(2) = -1,
@Hour NVARCHAR(2)=-1,
@TEID nvarchar(max) ,
@IgnoreIdlingTime int=120,
@DrivingTime int=300,--5 minutes by default
@CalculationFactor nvarchar(10)='speed'

My code to execute procedure and pass parameters: 我的代码执行过程并传递参数:

using (var context = new GPSModel())
{
    context.Database.Initialize(force: false);

    // Create a SQL command to execute the stored procedure
    var cmd = context.Database.Connection.CreateCommand();
    cmd.CommandText = "Procedure_Name";

    DbParameter TEIDParam = cmd.CreateParameter();
    TEIDParam.ParameterName = "@TEID";
    TEIDParam.DbType = System.Data.DbType.String;
    TEIDParam.Direction = ParameterDirection.Input;
    TEIDParam.Value = TEID;
    cmd.Parameters.Add(TEIDParam);

    context.Database.Connection.Open();

    var reader = cmd.ExecuteReader();
}

I tried to remove @ sign and send SqlParameter instead of DbParameter but still I have the same issue. 我试图删除@符号并发送SqlParameter而不是DbParameter但仍然遇到相同的问题。

Is there any other way to do that where my stored procedure is very complex and contains multi sets 在我的存储过程非常复杂并且包含多个集合的情况下,还有其他方法可以做到这一点

Thanks... 谢谢...

You can use the following code to solve the error. 您可以使用以下代码解决该错误。 I have added cmd.CommandType = CommandType.StoredProcedure; 我添加了cmd.CommandType = CommandType.StoredProcedure; . Now it works properly. 现在它可以正常工作了。

    var cmd = context.Database.Connection.CreateCommand();
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.CommandText = "Procedure_Name";
    DbParameter TEIDParam = cmd.CreateParameter();
    TEIDParam.ParameterName = "@TEID";
    TEIDParam.DbType = System.Data.DbType.String;
    TEIDParam.Direction = ParameterDirection.Input;
    TEIDParam.Value = TEID;
    cmd.Parameters.Add(TEIDParam);

When you are using stored procedure you have to set CommandType property to StoredProcedure , and then you should set the CommandText property to the name of the stored procedure. 使用存储过程时,必须将CommandType属性设置为StoredProcedure ,然后应将CommandText属性设置为存储过程的名称。 The command executes this stored procedure when you call one of the Execute methods. 当您调用Execute方法之一时,该命令将执行此存储过程。

FOR EF core it can be done using fromSQL() 对于EF核心,可以使用fromSQL()完成

var tbl = new DataTable();
           tbl.Columns.Add("id", typeof(string));

        foreach (var item in imageChunkRequest.ChunkNames)
        {
               tbl.Rows.Add(item);

        }
 SqlParameter Parameter = new SqlParameter();
                Parameter.ParameterName = "@udt";
                Parameter.SqlDbType = SqlDbType.Structured;
                Parameter.Value = tbl;
                Parameter.TypeName = "dbo.StringList";

     _dbContext.Set<T>().FromSql("EXEC dbo.FindChunks @udt",Parameter);

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

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