我不能在事务范围内从实体框架调用存储过程吗?

发布于 2024-08-19 05:05:10 字数 2312 浏览 10 评论 0原文

我有一个方法,使用实体框架在不同的实体中进行一些更改/插入,所有这些都在单个事务范围内。这些改变效果非常好。

当我需要在这些操作中间使用存储过程时,我的问题就开始了。该过程仅在一张表中执行插入操作,并且没有显式声明事务。我尝试过声明一个事务并在那里提交,但问题是一样的。

我无法在事务范围内从实体框架 (EF1) 调用存储过程吗?

仅当 using 块关闭时,才会在 transaction.Complete() 之后引发此异常。

The transaction has aborted.

   at System.Transactions.TransactionStatePromotedAborted.PromotedTransactionOutcome(InternalTransaction tx)
   at System.Transactions.TransactionStatePromotedEnded.EndCommit(InternalTransaction tx)
   at System.Transactions.CommittableTransaction.Commit()
   at System.Transactions.TransactionScope.InternalDispose()
   at System.Transactions.TransactionScope.Dispose()

内部异常:

The transaction operation cannot be performed because there are pending requests working on this transaction.

   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
   at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)
   at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
   at System.Data.SqlClient.TdsParser.TdsExecuteTransactionManagerRequest(Byte[] buffer, TransactionManagerRequestType request, String transactionName, TransactionManagerIsolationLevel isoLevel, Int32 timeout, SqlInternalTransaction transaction, TdsParserStateObject stateObj, Boolean isDelegateControlRequest)
   at System.Data.SqlClient.SqlInternalConnectionTds.ExecuteTransactionYukon(TransactionRequest transactionRequest, String transactionName, IsolationLevel iso, SqlInternalTransaction internalTransaction, Boolean isDelegateControlRequest)
   at System.Data.SqlClient.SqlInternalConnectionTds.ExecuteTransaction(TransactionRequest transactionRequest, String name, IsolationLevel iso, SqlInternalTransaction internalTransaction, Boolean isDelegateControlRequest)
   at System.Data.SqlClient.SqlDelegatedTransaction.SinglePhaseCommit(SinglePhaseEnlistment enlistment)

更新:开始赏金

今天我不再通过实体框架进行此过程调用。我通过 ADO.net 调用该过程,这是我的解决方法。但问题仍然存在,我应该需要在不久的将来进行一些调用,也许是在事务范围内。

I have a method that uses Entity Framework to do some changes/inserts in different entities, all this inside a single transaction scope. These changes works very well.

My problem has began when I needed to use a stored procedure in the middle of these operations. The procedure does only an insert in one table, and has no explicit declaration of transactions. I've tried declaring a transaction and commiting there also, but the problem was the same.

Can't I call a stored procedure from Entity Framework (EF1) inside a transaction scope?

This exception is thrown only after transaction.Complete(), when the using block is closed.

The transaction has aborted.

   at System.Transactions.TransactionStatePromotedAborted.PromotedTransactionOutcome(InternalTransaction tx)
   at System.Transactions.TransactionStatePromotedEnded.EndCommit(InternalTransaction tx)
   at System.Transactions.CommittableTransaction.Commit()
   at System.Transactions.TransactionScope.InternalDispose()
   at System.Transactions.TransactionScope.Dispose()

Inner exception:

The transaction operation cannot be performed because there are pending requests working on this transaction.

   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
   at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)
   at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
   at System.Data.SqlClient.TdsParser.TdsExecuteTransactionManagerRequest(Byte[] buffer, TransactionManagerRequestType request, String transactionName, TransactionManagerIsolationLevel isoLevel, Int32 timeout, SqlInternalTransaction transaction, TdsParserStateObject stateObj, Boolean isDelegateControlRequest)
   at System.Data.SqlClient.SqlInternalConnectionTds.ExecuteTransactionYukon(TransactionRequest transactionRequest, String transactionName, IsolationLevel iso, SqlInternalTransaction internalTransaction, Boolean isDelegateControlRequest)
   at System.Data.SqlClient.SqlInternalConnectionTds.ExecuteTransaction(TransactionRequest transactionRequest, String name, IsolationLevel iso, SqlInternalTransaction internalTransaction, Boolean isDelegateControlRequest)
   at System.Data.SqlClient.SqlDelegatedTransaction.SinglePhaseCommit(SinglePhaseEnlistment enlistment)

UPDATE: Started a bounty

Today I'm not doing this procedure call through Entity Framework anymore. I'm calling the procedure through ADO.net, this was my workaround. But the problem continues, I should need make some calls in the near future, maybe inside a transaction scope.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

江南烟雨〆相思醉 2024-08-26 05:05:10

我终于找到了一个解决方案...似乎 EF 期望存储过程(导入函数)返回一个值。
因此,当函数返回时,调用 .FirstOrDefault() 。

I Finally found a solution...it seems EF expects the stored proc (imported function) to return a value.
so call .FirstOrDefault() on the function when it returns.

維他命╮ 2024-08-26 05:05:10

hotifx 的错误? 知识库 936983

A bug with a hotifx? KB 936983

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文