我有一个sproc
CREATE PROCEDURE [dbo].[GetNextImageRequest]
(...) AS
DECLARE @ReturnValue BIT
SET @ReturnValue = 1
-- Set paranoid level isolation: only one access at a time!
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
BEGIN TRANSACTION
BEGIN TRY
...
UPDATE STATEMENT THAT THROWS FOREIGN KEY EXCEPTION
IF @@trancount > 0
BEGIN
COMMIT TRANSACTION
END
SET @ReturnValue = 0
END TRY
BEGIN CATCH
IF @@trancount > 0
BEGIN
ROLLBACK TRANSACTION
END
SET @ReturnValue = 1
END CATCH
SET TRANSACTION ISOLATION LEVEL READ COMMITTED
SET NOCOUNT ON
RETURN @ReturnValue -- 0=success
GO
当我从 Sql Server Management Studio 手动调用它时,我没有得到任何异常。当我通过 Entity Framework 6 调用它时,我得到
EXECUTE 之后的事务计数表明 BEGIN 和 COMMIT 语句的数量不匹配。先前计数 = 1,当前计数 = 0。
我究竟做错了什么?外键约束正在回滚,但我正在检查@@TRANCOUNT。