title: Passing Entity Framework object to helper method for update
body: <p>I'm having some trouble using a helper method to perform an update to a set of model objects. The table uses a lookup table to hold 5 records per agent/user. If I want to save the record for the agent, I need to save that record onto the <code>AgentTransmission</code> table, and up to 5 other records on the <code>RelationshipCodeLookup</code> table. </p>
  
  <p>Since I have to do this five times per agent, and we must do the process in the Create and Edit methods, I created a helper method to save the records. This works fine during the create process as we're simply doing a <code>DbContext.Add()</code>. However when I need to perform an update, I get the error message</p>
  
  <pre><code>An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key.
  </code></pre>
  
  <p>I think this has to do with the fact I'm passing the model object to my helper method, and therefore the <code>DbContext</code> thinking that it has two separate objects to keep track of. I say this because the lines of code that are commented out work just fine and allow me to save the object. Passing the object to the helper method, however, gets the above error. </p>
  
  <p>Does anyone know of a way around this (using a helper method to perform an update)? </p>
  
  <p><strong>Controller Action</strong></p>
  
  <pre><code>//Save relationship codes in lookup table
  if (AgentTransmissionValidator.ValidateRelationshipCode(agenttransmission.RelationshipCode1))
  {
      //db.Entry(agenttransmission.RelationshipCode1).State = EntityState.Modified;
      //db.SaveChanges();
      SaveRelationshipCodes(agenttransmission.RelationshipCode1, agenttransmission.ID);
  }
  
  if (AgentTransmissionValidator.ValidateRelationshipCode(agenttransmission.RelationshipCode2))
  {
      //db.Entry(agenttransmission.RelationshipCode1).State = EntityState.Modified;
      //db.SaveChanges();
      SaveRelationshipCodes(agenttransmission.RelationshipCode2, agenttransmission.ID);
  }
  
  if (AgentTransmissionValidator.ValidateRelationshipCode(agenttransmission.RelationshipCode3))
  {
      //db.Entry(agenttransmission.RelationshipCode1).State = EntityState.Modified;
      //db.SaveChanges();
      SaveRelationshipCodes(agenttransmission.RelationshipCode3, agenttransmission.ID);
  }
  
  if (AgentTransmissionValidator.ValidateRelationshipCode(agenttransmission.RelationshipCode4))
  {
      //db.Entry(agenttransmission.RelationshipCode1).State = EntityState.Modified;
      //db.SaveChanges();
      SaveRelationshipCodes(agenttransmission.RelationshipCode4, agenttransmission.ID);
  }
  
  if (AgentTransmissionValidator.ValidateRelationshipCode(agenttransmission.RelationshipCode5))
  {
      //db.Entry(agenttransmission.RelationshipCode1).State = EntityState.Modified;
      //db.SaveChanges();
      SaveRelationshipCodes(agenttransmission.RelationshipCode5, agenttransmission.ID);
  }
  </code></pre>
  
  <p><strong>Helper Method</strong></p>
  
  <pre><code>    public void SaveRelationshipCodes(RelationshipCodeLookup relCode, int id)
      {
          if (relCode.AgentId == 0) relCode.AgentId = id;
  
          relCode.LastChangeDate = DateTime.Now;
          relCode.LastChangeId = Security.GetUserName(User);
  
          //Check to see if record exists and if not add it
          if (db.RelationshipCodeLookup.Find(id, relCode.RelCodeOrdinal) != null)
          {
              db.Entry(relCode).State = EntityState.Detached;
          }
          else
          {
              if(relCode.RelCodeOrdinal == 0) relCode.RelCodeOrdinal = FindOrdinal(relCode);
              db.RelationshipCodeLookup.Add(relCode);
          }
  
          db.SaveChanges();
      }
  </code></pre>
  
  <p><strong>EDIT</strong></p>
  
  <p>After scouring the web I attempted to save via this method</p>
  
  <pre><code>        //Check to see if record exists and if not add it
          if (db.RelationshipCodeLookup.Find(id, relCode.RelCodeOrdinal) != null)
          {
              db.Entry(relCode).CurrentValues.SetValues(relCode);
          }
          else
          {
  
  Member 'CurrentValues' cannot be called for the entity of type 'RelationshipCodeLookup because the entity does not exist in the context. To add an entity to the context call the Add or Attach method of DbSet&lt;RelationshipCodeLookup&gt;
  </code></pre>
  
  <p>However.... doing that only puts me back at the start with the following error on <code>db.RelationshipCodeLookup.Attach(relCode);</code></p>
  
  <pre><code>An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key.
  </code></pre>