title: How to de-attach an entity from a Context in Entity Framework?
body: <p>I use EF 4.1 with Repository and DbContext.. POCO with T4 template.
  For every Repository I use a separate DbContext.</p>
  
  <p>I need to update an object with has a related property, at the moment I receive this error</p>
  
  <pre><code>An entity object cannot be referenced by multiple instances of IEntityChangeTracker.
  </code></pre>
  
  <p>I suppose my problem is beacuse <code>eventObj</code> and <code>candidate</code> are created from different Repositories.</p>
  
  <p>So i'm trying to solve the problem with this code, with no success.</p>
  
  <p>My question?</p>
  
  <ul>
  <li>How do I get rid of this error?</li>
  <li><p>Is is possible remove the candidate from its context?</p>
  
  <pre><code>public void UpdateAddingCandidate(Event eventObj, Candidate candidate){
      Event updatedEvent = new Event();
      Candidate updatedCandidate = new Candidate();
      updatedEvent = eventObj;
      updatedCandidate = candidate;
      updatedEvent.Candidate = updatedCandidate;
      db.Entry(updatedEvent).State = EntityState.Modified;     
  }
  </code></pre></li>
  </ul>
  
  <p>EDIT</p>
  
  <pre><code>    public void UpdateAddingCandidate(Event eventObj, Candidate candidate)
      {
          /*
          db.Events.AsNoTracking();
          db.Candidates.AsNoTracking();
          */
          db.Entry(eventObj).State = EntityState.Detached;
          db.Entry(candidate).State = EntityState.Detached;
  
          Event updatedEvent = new Event();
          Candidate updatedCandidate = new Candidate();
          updatedEvent = eventObj;
          updatedCandidate = candidate;
          updatedEvent.Candidate = updatedCandidate;
          db.Entry(updatedEvent).State = EntityState.Detached;
          db.Entry(updatedEvent).State = EntityState.Modified;
  
      }
  </code></pre>