title: NHibernate Illegally attempted to associate a proxy with two open Sessions
body: <p>I am stuck from about 12 hours with this error so could any one help me at least to understand what is wrong  </p>
  
  <p>Here is my code</p>
  
  <pre><code>public virtual void AssignAssets(IList&lt;long?&gt; assetsIds, ManualAssetAssignment&lt;TAsset&gt;.AssignmentMode mode, IQueryableRepository&lt;TAsset, long?&gt; assetRepository)
  {
      this.Assets.Clear();
      if (mode == AssignmentMode.Execlusion)
      {
          //IList&lt;long?&gt; includedAssetsIds = assetRepository.Find().Where(entity =&gt; !assetsIds.Contains(entity.Id)).Select(entity =&gt; entity.Id).ToList();
          IList&lt;TAsset&gt; includedAssets = assetRepository.Find().Where(entity =&gt; !assetsIds.Contains(entity.Id)).ToList();
          foreach (var asset in includedAssets)
          {
              this.Assets.Add(asset);
          }
      }
      else
      {
          foreach (var assetId in assetsIds)
          {
              var asset = assetRepository.Get(assetId);
              this.Assets.Add(asset);
          }
      }
      _areAssetsEvaluated = true;
  }
  </code></pre>
  
  <p>Here is my test </p>
  
  <pre><code>using (var tx = DataSessionContext.Session.BeginTransaction())
  {
      atm = new Atm(AtmTestData.AutoCompleteCommand(new Atm.SetupCommand() { }), commandDependencyRegistry);
      atmRepository.Save(atm);
      List&lt;long?&gt; atmIds = new List&lt;long?&gt; { atm.Id };
  
      var command = AtmExpenseTestData.AutoCompleteCommand(new AssetExpense&lt;Atm&gt;.SetupCommand());
      expense = new AssetExpense&lt;Atm&gt;
      (
          command, commandDependencyRegistry
      );
  
      var assignCommand1 = new AssetExpense&lt;Atm&gt;.ManualAssetAssignmentCommand()
      {
          AssetsIds = new List&lt;long?&gt;() { atm.Id.Value },
          Mode = ManualAssetAssignment&lt;Atm&gt;.AssignmentMode.Execlusion
      };
      assignCommand1.Execute(expense, commandDependencyRegistry);
      atmAssetExpenseRepository.Save(expense);
  
      tx.Commit();
  }
  </code></pre>
  
  <p>the weird thing is that if I replace this piece in my code </p>
  
  <pre><code>IList&lt;TAsset&gt; includedAssets = assetRepository.Find().Where(entity =&gt; !assetsIds.Contains(entity.Id)).ToList();
  foreach (var asset in includedAssets)
  {
      this.Assets.Add(asset);
  }
  </code></pre>
  
  <p>By this </p>
  
  <pre><code>IList&lt;long?&gt; includedAssetsIds = assetRepository.Find().Where(entity =&gt; !assetsIds.Contains(entity.Id)).Select(entity =&gt; entity.Id).ToList();
  foreach (var assetId in includedAssetsIds)
  {
      this.Assets.Add(assetRepository.Get(assetId));
  }
  </code></pre>
  
  <p>If I replace it the error disappear so could anyone help me to understand this error.</p>
  
  <p><strong>Edit - This might be helpful</strong></p>
  
  <p>From Stack Trace I get that the exception be thrown from this method specifically on line 13 <code>return (TId)((ISession)Session).Save(entity);</code></p>
  
  <pre><code>public virtual TId Save(TAggregateRoot entity, Action commitPostAction, Action rollbackPostAction, bool propagatePostActionError = false)
  {
      if (entity == null)
      {
          throw new ArgumentNullException("entity");
      }
  
      if (entity is IValidatable)
      {
          ((IValidatable)entity).Validate();
      }
      RegisterPostActions(commitPostAction, rollbackPostAction, propagatePostActionError);
      if (Session is ISession)
      {
          return (TId)((ISession)Session).Save(entity);
      }
      else
      {
          return (TId)((IStatelessSession)Session).Insert(entity);
      }
  }
  </code></pre>