我在我的應用程序中使用了Asp.net核心和EF核心。基本上我想從單個存儲過程中獲得多個結果集。試圖搜索它最後2天沒有這樣的運氣。試圖圍繞找出一個工作,解決它..使用EF Core獲取存儲過程的輸出參數值?
這是我的存儲過程:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[usp_CustomerAll_sel]
@SomeOutput int OUT
AS
BEGIN
SET NOCOUNT ON;
SELECT *
FROM [dbo].[Customer]
SELECT @SomeOutput = @@rowcount + 25 --This number 25 is a variable from a complex query but always an integer
END
我有2個記錄在該表中,所以基本上它應該返回客戶的類型和輸出參數表應該從我的.NET代碼返回27 ..
現在我迄今
[HttpGet]
public async Task<Tuple<IEnumerable<Customer>, int>> GetAllCustomer()
{
var votesParam = new SqlParameter
{
ParameterName = "SomeOutput",
Value = -1,
Direction = ParameterDirection.Output
};
var y = await _customerContext.Customers.FromSql("usp_CustomerAll_sel @SomeOutput out", votesParam).ToArrayAsync();
return new Tuple<IEnumerable<Customer>, int>(y, (int)votesParam.Value);
}
以上人試圖返回我的名單,但我沒有得到OUTP的價值從DB .(int)votesParam.Value
UT參數顯示空
現在,如果我用ExecuteNonQueryAsync
,然後我得到的輸出參數,但不是實際的數據
private async Task ExecuteStoredProc()
{
DbCommand cmd = _customerContext.Database.GetDbConnection().CreateCommand();
cmd.CommandText = "dbo.usp_CustomerAll_sel";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@SomeOutput", SqlDbType.BigInt) { Direction = ParameterDirection.Output, Value = -1 });
if (cmd.Connection.State != ConnectionState.Open)
{
cmd.Connection.Open();
}
await cmd.ExecuteNonQueryAsync();
long SomeOutput = (long)cmd.Parameters["@SomeOutput"].Value;
}
有沒有什麼辦法讓這兩個結果集和輸出參數並返回爲一個元組?
當我只是把硬編碼值,那麼它看起來像
[HttpGet]
public async Task<Tuple<IEnumerable<Customer>, int>> GetAllCustomer()
{
var votesParam = new SqlParameter
{
ParameterName = "SomeOutput",
Value = -1,
Direction = ParameterDirection.Output
};
var y = await _customerContext.Customers.FromSql("usp_CustomerAll_sel @SomeOutput out", votesParam).ToArrayAsync();
return new Tuple<IEnumerable<Customer>, int>(y, **25**);
}
,並導致像
{"item1":[{"customerId":1,"customerName":"Cus1"},{"customerId":2,"customerName":"Cus2"}],"item2":27}
基本上這就是我期待的...任何幫助嗎?
我知道有在ADP.net一些可能性,但嘗試使用EF核心來實現它。不知何故,我的架構師喜歡用它來等待異步web api動作 –