6

エンティティ フレームワークに変更を保存しようとすると、次のエラーが発生します -

System.Data.SqlClient.SqlException: セッションで他のスレッドが実行されているため、新しいトランザクションは許可されません。

この問題に対するさまざまな回答を見てきましたが、それらのどれも機能していないようです。基本的に、リポジトリ内のトランザクションで多数のアイテムを保存しています。いくつかのアイテムをループしてそれらを削除し、監査記録。

これについて私が見た他のすべての回答(例:Mark Staffords Answer)は、明示的なトランザクション(私が持っている)を宣言するか、ループの完了後にのみ変更の保存を呼び出すことを提案しています(これは、監査が現在機能している方法のためオプションではありません-監査監査詳細レコードを書き込むには ID が必要です)。

「SaveChanges」が削除メソッド内で呼び出されるたびに、エラーがスローされます。以下を参照してください -

public virtual void Save(DoseReturn oldDoseReturn)
{
    // Get the datetime when the save started
    DateTime saveStartTime = DateTime.Now;
    Dictionary<string, object> oldValues = new Dictionary<string, object>();
    Dictionary<string, object> newValues = new Dictionary<string, object>();

    // Get the object context and open a new transaction
    ObjectContext objectContext = ((IObjectContextAdapter)context).ObjectContext;
    objectContext.Connection.Open();
    DbTransaction transaction = objectContext.Connection.BeginTransaction();

    // Use the transaction for all updates
    using (transaction)
    {
        if (oldDoseReturn != null)
        {
              IDoseReturnStatusRepository statusRepository = new DoseReturnStatusRepository();
              var list = statusRepository.AsQueryable().Where(x => x.DoseReturnID == oldDoseReturn.DoseReturnID);

              foreach (var item in list)
              {
                  statusRepository.Delete(item, objectRetrievedDateTime, objectContext, saveStartTime, out oldValues, out newValues);
              }

              context.SaveChanges();

              // Get the relevant repository
              IDoseReturnsRepository repository = new DoseReturnsRepository();

              // audit and delete the object
              repository.Delete(oldDoseReturn, objectRetrievedDateTime, objectContext, saveStartTime, out oldValues, out newValues);

              context.SaveChanges();
         }
    }

    try
    {
         // Conduct a final save, then commit the transaction
         context.SaveChanges();
         transaction.Commit();
    }
    catch (Exception ex)
    {
         // An error has occurred, rollback the transaction and close the connection, then present the error
         transaction.Rollback();
         objectContext.Connection.Close();
         throw ex;
    }
    // Close the connection
    objectContext.Connection.Close();
}

public virtual void Delete(T entity, DateTime? objectRetrievedDateTime, ObjectContext objectContext, DateTime saveStartTime, out Dictionary<string, object> oldValues, out Dictionary<string, object> newValues)
    {
        oldValues = new Dictionary<string, object>();
        newValues = new Dictionary<string, object>();

        if (entity == null)
        {
            throw new ArgumentException("Cannot update a null entity.");
        }

        string entityName = entity.GetType().Name;

        if (!objectRetrievedDateTime.HasValue || !this.AuditsAfterRetieval(objectRetrievedDateTime, entityName, entity, saveStartTime))
        {
            this.DeletedEntityAudit(entity, out oldValues, out newValues);

            context.Entry(entity).State = System.Data.EntityState.Deleted;
            this.context.Set<T>().Remove(entity);
            this.Audit(entity, entityName, "Delete", oldValues, newValues, true);
            this.context.SaveChanges();
        }
        else
        {
            throw new Exception("Object cannot be saved as it has been amended in another thread");
        }
    }
4

1 に答える 1