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

时间:2017-08-30 17:39:34

标签: c# sql-server entity-framework stored-procedures asp.net-core-1.1

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

enter image description here

我正在调用这样的存储过程:

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();

这是我的模型类,应该用存储过程的返回值填充:

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中调用存储过程,它不会返回值 - 只是一条消息。

enter image description here

为了获得结果,我声明了一个变量并将其分配给存储过程。

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;

enter image description here

如何调用存储过程来返回值?

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();

1 个答案:

答案 0 :(得分:1)

RETURN不会输出SP值,只返回要放在调用数据库对象上的变量上的数据。这就是EXECUTE有效的原因,但运行SP本身只会返回一个&#34;命令已成功完成。&#34;

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