我正嘗試使用實體框架運行以下事務。在事務範圍內,我從DB調用存儲過程。使用實體框架在事務內部調用存儲過程
using (mother_Entities entitiesContext = context.Value)
{
using (var transactionScope = new TransactionScope())
{
// a lot of create, insert, update operations goes here
...
entitiesContext.SaveChanges();
//Execute stored procedure:
var paramMessage = new ObjectParameter("MESSAGE", "");
var paramMotherid = new ObjectParameter("MOTHERID", motherProductId);
var paramTochteridlist = new ObjectParameter("TOCHTER_ID_LIST", string.Join(";", motherIds));
var paramError = new ObjectParameter("ERROR", typeof(int));
var paramErrorText = new ObjectParameter("ERR_TEXT", typeof(string));
entitiesContext.ExecuteFunction("SP_DOCUWARE_UPDATE", paramMessage, paramMotherid,
paramTochteridlist, paramError, paramErrorText);
...
transactionScope.Complete();
}
}
上線entitiesContext.ExecuteFunction()
我得到異常Transaction count after EXECUTE indicates a mismatching number of BEGIN and COMMIT statements. Previous count = 1, current count = 0
我的存儲過程不使用任何交易,不調用任何其他函數或過程。所以我不明白爲什麼我不能在事務內執行事務過程。
UPDATE:
哦,我發現這個存儲過程:
...
IF @COMMIT = 1
BEGIN
IF @CANCEL = 1
ROLLBACK
ELSE
COMMIT
END
ELSE IF @CHECK = 1
ROLLBACK
END
...
可能被提交後會拋出異常。但如何逃避這個錯誤?
我認爲這不是用TransactionScope,而是用Stored proc。請檢查存儲過程是否與您在呼叫之前傳遞/設置的參數一起工作。 –
你的意思是說「正在使用參數」?我更新了我的問題。 – algreat
我的意思是,具有相同值的profiler trace具有相同的值,能夠在SQL server Management Studio中運行異常... –