title: dbContext An entity object cannot be referenced by multiple instances of IEntityChangeTracker
body: <p>So the problem I'm having is the following. When going into this if statement, then I want to delete all the entries with ClearDbInstallForGroupAndHeader() from table InstallationBOM. Then I want to add all the entries within NewInstallationBoms into my InstallationBOM table and while doing this calculating total cost, but when I want to add it with this line <strong>db.InstallationBOMs.Add(newInstallationBom);</strong>, I get the following error:<br>
      <strong>An entity object cannot be referenced by multiple instances of IEntityChangeTracker.</strong></p>
  
  <pre><code> if (InstallationSelected)
   {
        bool newEntry = true;
        if (SelectedQuoteDetails != null)
        {
            QuoteDetail theQuoteDetail = SelectedQuoteDetails.FirstOrDefault(X =&gt; X.GroupNr == groupID &amp;&amp; X.LineType == "I");
            if (theQuoteDetail != null)
            {
                 this.ClearDbInstallForGroupAndHeader();
                 int position = SelectedQuoteDetails.IndexOf(theQuoteDetail);
                 newEntry = false;
                 theQuoteDetail.Quantity = InstallQty;
                 theQuoteDetail.UnitCost = InstallCost;
                 theQuoteDetail.UnitPrice = InstallPrice;
                 SelectedQuoteDetails[position] = theQuoteDetail;
  
                 db.QuoteDetails.Attach(theQuoteDetail);
                 db.Entry(theQuoteDetail).State = EntityState.Modified;
                 db.SaveChanges();
  
                decimal totalInstallationCost = 0;
                totalInstallationCost = AddInstallToDbCalcTotalCost(theQuoteDetail.Line.Value, groupID);
  
                newLines.UnitCost = totalInstallationCost;
                newLines.UnitPrice = newLines.UnitCost * (1 + (Settings.MarginPercentage / 100));
  
                InstallCost = newLines.UnitCost ?? 0;
                InstallPrice = newLines.UnitPrice ?? 0;                                        
  
                RaisePropertyChanged(() =&gt; SelectedQuoteDetails);
  
            }
        }
    }
  
    public void ClearDbInstallForGroupAndHeader()
      {
          try
          {
              using (QuoteConfiguratorEntities db = Utilities.GetContext())
              {
                  List&lt;InstallationBOM&gt; dbList = db.InstallationBOMs.Where(x =&gt; x.QuoteHeaderId == this.SelectedQuoteHeader.ID &amp;&amp; x.GroupNr == this.SelectedGroup.ID)
                                                          .ToList();
                  foreach (InstallationBOM existingInstallationBom in dbList)
                  {
                      db.InstallationBOMs.Remove(existingInstallationBom);
                  }
                  db.SaveChanges();
              }
  
          }
          catch (Exception ex)
          {
              this.Message = ex.Message + "\n\n" + ex.StackTrace;
              this.ShowErrorMessage();
          }
      }
  
      public decimal AddInstallToDbCalcTotalCost(decimal pLine, long pGroupNr)
      {
          decimal totalInstallationCost = 0;
          try
          {
              using (QuoteConfiguratorEntities db = Utilities.GetContext())
              {
                  foreach (InstallationBOM newInstallationBom in this.NewInstallationBoms)
                  {
                      newInstallationBom.QuoteHeaderId = this.SelectedQuoteHeader.ID;
                      newInstallationBom.GroupNr = pGroupNr;
                      newInstallationBom.LineNumber = pLine;
                      totalInstallationCost += (newInstallationBom.Quantity.Value *
                                                newInstallationBom.Cost.Value);
                      db.InstallationBOMs.Add(newInstallationBom);                       
                      db.SaveChanges();   
                  }
              }
          }
          catch (Exception ex)
          {
              this.Message = ex.Message + "\n\n" + ex.StackTrace;
              this.ShowErrorMessage();
          }
          return totalInstallationCost;
      }
  
  public void OpenInstallationOptionsWindow(bool AsThread = false)
      {
          try
          {
              if (AsThread)
              {
                  this.Busy = true;
                  this._Thread = new Thread(() =&gt; QuoteConfiguratorViewModel.OpenInstallationOptionsWindow(this));
                  this._Thread.IsBackground = true;
                  this._Thread.Start();
              }
              else
              {
                  using (QuoteConfiguratorEntities db = Utilities.GetContext())
                  {
                      ObservableCollection&lt;InstallationBOM&gt; installBomListGroup = new ObservableCollection&lt;InstallationBOM&gt;(db.InstallationBOMs.Where(x =&gt; x.QuoteHeaderId == this.SelectedQuoteHeader.ID
                          &amp;&amp; x.GroupNr == this.SelectedGroup.ID).ToList());
                      if (installBomListGroup != null)
                      {
                          if (!installBomListGroup.Any())
                          {
                              ClearInstallationBomEntry();
                              NewInstallationBoms = new ObservableCollection&lt;InstallationBOM&gt;();
                          }
                          else if (installBomListGroup.Any())
                          {
                              NewInstallationBoms = installBomListGroup;
                          }
                      }                        
  
                      Messenger.Default.Send&lt;OpenInstallationWindow&gt;(new OpenInstallationWindow());
  
                  }
  
              }
          }
          catch (Exception ex)
          {
              this.Message = Utilities.GetError(ex);
          }
      }
  </code></pre>