我正在尝试使用实体框架运行以下事务。在事务范围内,我从数据库调用存储过程。
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
我的存储过程不使用任何事务,也不调用任何其他函数或过程。所以我不明白为什么我不能在事务中执行存储过程。
更新:
哦,我在存储过程中找到了这个:
...
IF @COMMIT = 1
BEGIN
IF @CANCEL = 1
ROLLBACK
ELSE
COMMIT
END
ELSE IF @CHECK = 1
ROLLBACK
END
...
可能是在提交异常被抛出之后。但是如何避免这个错误呢?