用于其他方法时的DBTransaction关闭

本文关键字:DBTransaction 关闭 其他 方法 用于 | 更新日期: 2023-09-27 18:24:18

我正试图在我的实体框架存储库中为多个不同的插入/更新语句使用一个事务,但每当我将事务传递给不同的方法时,它都会返回为关闭,请参阅下面的-

ObjectContext objectContext = ((IObjectContextAdapter)context).ObjectContext;
objectContext.Connection.Open();
DbTransaction transaction = objectContext.Connection.BeginTransaction();
using (transaction)
{
      IPersonRepository personRepository = new PersonRepository();
      context.Entry(person).State = System.Data.EntityState.Modified;
      personRepository.Update(person, objectRetrievedDateTime, transaction, objectContext);
      if (existingStatus != null)
      {
           objectContext.CreateObjectSet<tblPersonStatus>().Attach(existingStatus);
           existingStatus.EndDate = DateTime.Now;
           context.Entry(existingStatus).State = System.Data.EntityState.Modified;
           IPersonStatusesRepository repository = new PersonStatusesRepository();
           repository.Update(existingStatus, objectRetrievedDateTime, transaction, objectContext);
      }
}

当第一个更新方法(personRepository.update)完成时,事务出现错误"base{System.SystemException}={"This SqlTransaction已完成;它不再可用。"}"

有什么办法绕过这个吗?

EDIT-被调用的更新方法看起来像这样-

public virtual void Update(T entity, DateTime? objectRetrievedDateTime, DbTransaction transaction, ObjectContext objectContext)
    {
        if (entity == null)
        {
            throw new ArgumentException("Cannot update a null entity.");
        }
        using (transaction)
        {
            ObjectStateEntry entry = objectContext.ObjectStateManager.GetObjectStateEntry(entity);
            string entityName = entity.GetType().Name;
            if (!objectRetrievedDateTime.HasValue || !this.AuditsAfterRetieval(objectRetrievedDateTime, entityName, entity))
            {
                Dictionary<string, object> oldValues = new Dictionary<string, object>();
                Dictionary<string, object> newValues = new Dictionary<string, object>();
                bool changed = this.EntityHasChanged(entity, entry, out oldValues, out newValues);
                // Check for changes before saving
                if (changed)
                {
                    this.context.SaveChanges();
                    this.Audit(entity, entityName, "Update", oldValues, newValues, false, null);
                }
            }
            else
            {
                throw new Exception("Object cannot be saved as it has been amended in another thread");
            }
        }
    }

用于其他方法时的DBTransaction关闭

您的问题是这个构造:

using (transaction)
{
   ...
  Update(transaction, ....)
}

当您退出它时,事务将被处置,因此也将变为无效。

只需删除更新方法中的using语句

您正在使用处理其中的事务

这是如何使用事务的快速存根

using(DbConnection connection = ...)    
{    
    connection.Open();    
    using(DbTransaction transaction = connection.BeginTransaction(...))    
    {
      try{
        ... update ...
        ... another update ...
        transaction.Commit(); 
      }catch(Exception){
       // transaction rolled back here if an Exception is thrown before the call to Commit()
        transaction.Rollback()
      }   
    }     
} // connection closed here