当用于另一个方法时,DBTransaction关闭

时间:2013-07-23 10:40:31

标签: c# asp.net entity-framework transactions

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

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} = {”此SqlTransaction已完成;它不再可用了。“}”

有没有办法解决这个问题?

编辑 - 调用的更新方法如下所示 -

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");
            }
        }
    }

2 个答案:

答案 0 :(得分:2)

你的问题是这个结构:

using (transaction)
{
   ...

  Update(transaction, ....)
}

退出时,交易为Disposed,因此也会失效。

答案 1 :(得分:1)

只需删除Update方法中的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