Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Entity framework Database.SqlQuery调用具有多个输出参数的存储过程_Entity Framework_Code First - Fatal编程技术网

Entity framework Database.SqlQuery调用具有多个输出参数的存储过程

Entity framework Database.SqlQuery调用具有多个输出参数的存储过程,entity-framework,code-first,Entity Framework,Code First,我有一个如下所示的存储过程,它接受1个输入参数(Name)并返回2个输出参数(EmployeeId和Salary)。我们的存储过程将把Name插入Employee表,并返回EmployeeId和Salary CREATE PROCEDURE dbo.insertemployee @iName varchar(500), @OEmployeeId int OUTPUT, @OSalary Money OUTPUT 我们使用EF代码优先的方法。我能够将记录插入employee表,但无法找到如何

我有一个如下所示的存储过程,它接受1个输入参数(Name)并返回2个输出参数(EmployeeId和Salary)。我们的存储过程将把Name插入Employee表,并返回EmployeeId和Salary

CREATE PROCEDURE dbo.insertemployee
@iName varchar(500),
@OEmployeeId int OUTPUT,  
@OSalary Money OUTPUT
我们使用EF代码优先的方法。我能够将记录插入employee表,但无法找到如何访问两个输出参数。我知道我需要像下面这样使用。谁能告诉我结果会怎样。根据MSDN,它可以是以列名作为属性的类。但我的情况是,我们不返回表中的列,而是使用两个输出参数,我需要知道如何访问这两个输出参数@OEmployeeId和@OSalary

context.Database.SqlQuery<Result>(" exec dbo.insertemployee....);

public class Result
{
   // what properties I must give here       
}
context.Database.SqlQuery(“exec dbo.insertemployee…”);
公开课成绩
{
//我必须在这里提供什么属性
}

您尝试使用的方法仅适用于查询结果。它无法自动将输出参数的值放入新对象中

在存储过程运行后,必须显式创建参数并读取其值

因此,如果您有这样一个存储过程:

CREATE PROCEDURE dbo.insertemployee
(
    @iName varchar(500),
    @OEmployeeId int OUTPUT,  
    @OSalary Money OUTPUT
)
AS
BEGIN
    SELECT @OEmployeeId = 1337;
    SELECT @OSalary = 1000;
END
using (var ctx = new Context())
{
    var nameParam = new SqlParameter("iName", "TestName");

    var employeeIdParam = new SqlParameter("OEmployeeId", SqlDbType.Int) 
    { 
        Direction = System.Data.ParameterDirection.Output 
    };

    var salaryParam = new SqlParameter("OSalary", SqlDbType.Money) 
    { 
        Direction = System.Data.ParameterDirection.Output 
    };

    ctx.Database.ExecuteSqlCommand(
        "insertemployee @iName, @OEmployeeId out, @OSalary out", 
        nameParam, employeeIdParam, salaryParam);

    var employeeId = (int)employeeIdParam.Value;
    var salary = (decimal)salaryParam.Value;
}
…您可以执行它并获得如下参数的结果:

CREATE PROCEDURE dbo.insertemployee
(
    @iName varchar(500),
    @OEmployeeId int OUTPUT,  
    @OSalary Money OUTPUT
)
AS
BEGIN
    SELECT @OEmployeeId = 1337;
    SELECT @OSalary = 1000;
END
using (var ctx = new Context())
{
    var nameParam = new SqlParameter("iName", "TestName");

    var employeeIdParam = new SqlParameter("OEmployeeId", SqlDbType.Int) 
    { 
        Direction = System.Data.ParameterDirection.Output 
    };

    var salaryParam = new SqlParameter("OSalary", SqlDbType.Money) 
    { 
        Direction = System.Data.ParameterDirection.Output 
    };

    ctx.Database.ExecuteSqlCommand(
        "insertemployee @iName, @OEmployeeId out, @OSalary out", 
        nameParam, employeeIdParam, salaryParam);

    var employeeId = (int)employeeIdParam.Value;
    var salary = (decimal)salaryParam.Value;
}

非常感谢Peter Hansen。这解决了我的问题。对于我来说,很难找到任何关于首先使用EF代码调用具有输出参数的存储过程的帖子和博客。非常感谢Peter Hanson,我正试图这样做,但我忘记了查询中的“out”,非常感谢!