EF6将每个存储过程调用包装在其自己的事务中。如何预防呢? [英] EF6 wraps every single stored procedure call in its own transaction. How to prevent this?

查看:92
本文介绍了EF6将每个存储过程调用包装在其自己的事务中。如何预防呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用SQL Server Profiler进行配置:EF 6使用 BEGIN TRAN COMMIT TRAN 包装每个存储过程调用。

Profiled with SQL Server Profiler: EF 6 wraps every single stored procedure call with BEGIN TRAN and COMMIT TRAN.

这不是重大更改吗?

也许这不仅是重大更改,而且使任何交易SP中的逻辑不可能,因为我们从不可以使用 ROLLBACK TRAN 在存储过程中回滚事务(注意:SQL Server中没有嵌套事务) ,因此回滚回滚到 @@ TRANCOUNT 零。因为我们在进行事务处理,因为EF 6,所以我们得到了执行EXECUTE之后的事务计数表明BEGIN和COMMIT语句的数量不匹配。上一个计数= 1,当前计数=0。标准的SQL Server错误。

Maybe it is not only a breaking change, but makes any transactional logic impossible in SPs as we never can rollback our transaction in the stored procedure using ROLLBACK TRAN (note: there are no nested transactions in SQL Server), so one rollback rollbacks to @@TRANCOUNT zero. As we were in a transaction because EF 6 we got "Transaction count after EXECUTE indicates a mismatching number of BEGIN and COMMIT statements. Previous count = 1, current count = 0." standard SQL Server error.

请不要问我为什么要调用存储过程。我有数百个,并且所有人都在使用 TRY ... COMMIT ...捕捉回退逻辑。

Please do not ask me why I want to call stored procedures. I have hundreds, and all of them are using TRY ... COMMIT ... CATCH ROLLBACK logic.

有什么想法可以防止EF 6这样做吗?

Any ideas how can I prevent EF 6 to do this?

推荐答案

ExecuteSqlCommand 方法的重载阻止了此行为:

There is an overload of the ExecuteSqlCommand method that prevents this behavior:

db.Database.ExecuteSqlCommand(TransactionalBehavior.DoNotEnsureTransaction, sql, parameters);

这篇关于EF6将每个存储过程调用包装在其自己的事务中。如何预防呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆