如何使用 Entity Framework 和 .Net Core 1.1 调用 returns 数据的存储过程

How to call stored procedure which returns data using Entity Framework and .Net Core 1.1

我正在尝试调用一个 return 值的存储过程。但出于某种原因,它没有 returning 值,而是抛出错误:

我这样调用存储过程:

var arTransactionid = context.Set<ARTransaction>()
                             .FromSql("core.ARTransaction_Insert @PersonID = {0},@ContractID = {1},@TransactionCodeID = {2},@TransactionDate = {3}," +
        "@TransactionDesc = {4},@Amount = {5},@CurrencyID = {6},@ExchangeRate = {7}," +
        "@BaseAmount = {8},@PostedDate = {9},@DueDate = {10},@Reference = {11}," +
        "@Reference2 = {12},@Reversal = {13},@BaseAdjustment = {14},@BatchID = {15}," +
        "@ParentTransactionID = {16},@InvoiceID = {17},@UserID = {18}", 46736, null, 197, "2017-08-25 00:00:00 -05:00", null, 501.0000, 2, 1, 501.0000, null, null, null, null, 0, 0, null, 0, null, 3052)
                             .FirstOrDefault();

这是我的模型 class,应该用存储过程中的 return 值填充:

public class ARTransaction
{
    public String arTransactionID { set; get; }
}

这是存储过程:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER Procedure [core].[ARtransaction_Insert]
    @PersonID int,
    @ContractID int,
    @TransactionCodeID int,
    @TransactionDate date,
    @TransactionDesc nvarchar(100),
    @Amount money,
    @CurrencyID int,
    @ExchangeRate decimal(18,6),
    @BaseAmount money,
    @PostedDate DATE = null,
    @DueDate date,
    @Reference nvarchar(50),
    @Reference2 nvarchar(50),
    @Reversal BIT = 0,
    @BaseAdjustment BIT = 0,
    @BatchID int,
    @ParentTransactionID int,
    @InvoiceID INT = null,
    @UserID INT,
    @SessionGUID UNIQUEIDENTIFIER = null
AS
    SET NOCOUNT ON

    DECLARE @ARtransactionID INT,
            @Valid INT,
            @ValidMessage NVARCHAR(100)

    --More code goes here ***********

    RETURN @ARtransactionID

如果我在 SQL Server Management Studio 中调用存储过程,它不会 return 值 - 只是一条消息。

为了得到一个结果,我声明了一个变量,并赋值给存储过程。

DECLARE @arTransctionInserted int;

EXEC @arTransctionInserted = core.ARTransaction_Insert 
          @PersonID = 46736,
          @ContractID = NULL,
          @TransactionCodeID = 197,
          @TransactionDate = '2017-08-25 00:00:00 -05:00',
          @TransactionDesc = NULL,
          @Amount = 500.0000,
          @CurrencyID = 2,
          @ExchangeRate = 1, 
          @BaseAmount = 500.0000,
          @PostedDate = NULL,
          @DueDate = NULL,
          @Reference = NULL,
          @Reference2 = NULL,
          @Reversal = 0,
          @BaseAdjustment = 0,
          @BatchID = NULL,
          @ParentTransactionID = 0,
          @InvoiceID = NULL,
          @UserID = 3052;

SELECT @arTransctionInserted as IDTransaction;

如何调用存储过程来 return 值?

context.Set<ARTransaction>().FromSql("")

我已经尝试调用它,包括所有查询,但错误不断。

var arTransactionid = context.Set<ARTransaction>().FromSql("DECLARE @arTransctionInserted int; exec @arTransctionInserted = core.ARTransaction_Insert @PersonID = {0},@ContractID = {1},@TransactionCodeID = {2},@TransactionDate = {3}," +
               "@TransactionDesc = {4},@Amount = {5},@CurrencyID = {6},@ExchangeRate = {7}," +
               "@BaseAmount = {8},@PostedDate = {9},@DueDate = {10},@Reference = {11}," +
               "@Reference2 = {12},@Reversal = {13},@BaseAdjustment = {14},@BatchID = {15}," +
               "@ParentTransactionID = {16},@InvoiceID = {17},@UserID = {18};SELECT @arTransctionInserted as IDTransaction;", 46736, null, 197, "2017-08-25 00:00:00 -05:00", null, 501.0000, 2, 1, 501.0000, null, null, null, null, 0, 0, null, 0, null, 3052).FirstOrDefault();

RETURN 不会输出 SP 值,只会 return 将数据放在调用数据库对象的变量上。这就是 EXECUTE 有效但 运行 SP 本身只会 return 一个 "Command completed successfully."

的原因

使用 SELECT @ARtransactionID; 因为这会输出您要查找的 ID。