私はAsp.Net MVCとEntity Frameworkを使用しています。私は次のように要求ごとのトランザクションを管理するためのクラスを持っている:EntityFrameworkの複数のdbcontextでのリクエストごとのトランザクション
public class TransactionPerRequest :
IRunOnEachRequest, IRunOnError, IRunAfterEachRequest
{
private readonly ContextDB _Context;
private readonly HttpContextBase _HttpContext;
public TransactionPerRequest(ContextDB context,
HttpContextBase httpContext)
{
_Context = context;
_HttpContext = httpContext;
}
void IRunOnEachRequest.Execute()
{
_HttpContext.Items["_Transaction"] =
_Context.Database.BeginTransaction(IsolationLevel.ReadCommitted);
}
void IRunOnError.Execute()
{
_HttpContext.Items["_Error"] = true;
}
void IRunAfterEachRequest.Execute()
{
var transaction = (DbContextTransaction)_HttpContext.Items["_Transaction"];
if (_HttpContext.Items["_Error"] != null)
transaction.Rollback();
else
transaction.Commit();
}
}
は私がTransactionPerRequset
クラスを変更するにはどうすればよいhere
が説明したように、複数のコンテキストを使用するようにしたいですか?私はこれを行っている
接続の初期化、開閉、処理および廃棄について説明できますか?ありがとう。 –
私はデータベース接続を扱う基本を意味しました。あなたはそれらを開いたままにしておきたい、または破棄したくはありません。 –