我正在使用.NET 3.5 SP1。如何在實體框架中獲取存儲過程@@ ERROR值?
我有一個存儲過程返回一個交易的結果如下圖所示:
Create PROCEDURE SetPrice
@itemId int,
@price int
AS
DECLARE @myERROR int -- Local @@ERROR
, @myRowCount int -- Local @@ROWCOUNT
SET NOCOUNT ON
BEGIN TRAN1
UPDATE item_price_table SET [email protected] WHERE [email protected]
UPDATE order_table SET [email protected] WHERE [email protected]
SELECT @myERROR = @@ERROR, @myRowCount = @@ROWCOUNT
IF @myERROR != 0 GOTO HANDLE_ERROR
COMMIT TRAN1 -- No Errors, so go ahead
RETURN 0
HANDLE_ERROR:
ROLLBACK TRAN1
RETURN @myERROR
我試圖讓@@ ERROR狀態代碼返回。在EF V1,I創建FunctionImport 'SetPrice' 和通過創建在 'mycontext' 類方法稱之爲如下所示:
public int SetPrice(int itemId, int price) {
Int32 result = -1;
EntityCommand cmd = ((EntityConnection)this.Connection).CreateCommand();
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.CommandText = "myContext.SetPrice";
cmd.Parameters.AddWithValue("itemId", itemId);
cmd.Parameters.AddWithValue("price", price);
EntityParameter retParameter = new EntityParameter();
retParameter.Direction = System.Data.ParameterDirection.ReturnValue;
cmd.Parameters.Add(retParameter);
cmd.Connection.Open();
cmd.ExecuteNonQuery();
result = Convert.ToInt32(retParameter.Value);
cmd.Connection.Close();
return result;
}
這導致以下錯誤:
The data reader returned by the store data provider does not have enough columns for the query requested. at System.Data.EntityClient.EntityCommandDefinition.ConstantColumnMapGenerator.System.Data.EntityClient.EntityCommandDefinition.IColumnMapGenerator.CreateColumnMap(DbDataReader reader) at System.Data.EntityClient.EntityCommandDefinition.Execute(EntityCommand entityCommand, CommandBehavior behavior) at System.Data.EntityClient.EntityCommand.ExecuteReader(CommandBehavior behavior) at System.Data.EntityClient.EntityCommand.ExecuteScalar[T_Result](Func`2 resultSelector) at System.Data.EntityClient.EntityCommand.ExecuteScalar()
請告訴如何解決問題。
謝謝。
+1完全同意,@@ ERROR在許多方面令人難以置信。 – 2009-08-18 21:17:39
謝謝。 我已經創建了示例存儲過程來探索EF v1中的SP支持。 – dev 2009-08-19 22:11:20