0

Web アプリケーションで EF4 の ObjectContext に Unif Of Work を実装しようとしています。UoW は HttpModule です。必要なのは、接続の現在のトランザクションを取得することです。http リクエストが最初に来ると、objectContext で context.Connection.BeginTransaction() を使用してトランザクションを開始します。リクエストの最後で、接続の現在のトランザクションを取得する必要がありますが、それを行うためのプロパティが Connection オブジェクトにありません。それを達成するために次のコードを作成しましたが、機能しません。

private DbTransaction GetTransaction()
             {
                      if (_currentTransaction == null)
                      {
                               var command = GetSession().Connection.CreateCommand(); // just to get the current transaction
                               if (command.Transaction != null)
                                        _currentTransaction = command.Transaction;
                               else
                                        _currentTransaction = GetSession().Connection.BeginTransaction();
                      }

                      return _currentTransaction;
             }

command.Transactionが常に null である理由がわかりません。GetSession().Connection.BeginTransaction() を実行しようとすると、トランザクションが既に存在し、複数のトランザクションを並行して開始できないという例外が発生します。

GetSession() は、HttpContext.Current.Items から現在の EF ObjectContext だけを取得します。ObjectContext は beginRequest でそこに格納されます。

ご指導いただければ幸いです。

ありがとう。

4

1 に答える 1

0

ObjectContext.Connectionストア接続ではありません。それはEntityConnectionです。

あなたはおそらくしたいですTransactionScope。ストアのみでトランザクションを開始する場合は、((EntityConnection)Context.Connection).StoreConnection.

于 2011-02-07T16:57:23.503 に答える