我正在使用Asp.Net MVC和Entity Framework。我有一个类来管理每个请求的事务,如下所示:
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();
}
}
我想按照here
的说明使用多个上下文如何更改TransactionPerRequset
课程?