title: Can't save record using Entity Framework
body: <p>My objects will not save no matter what I do they will fetch and get info and make a new record but not update.</p>
  
  <p>This is the code that details with getting existing patient and then modifying the record setting the state then calling save change this is cracking my head the last three hours what is going wrong. I was told you had to change the entity state of an object before it would no if to save but when i try to attach it it says its already attached </p>
  
  <pre><code>Appointment _appointment = new Appointment();
  int errorCount = 0;
  
  Patient _patient = SourceDal.getPatientByPatientNewId(Convert.ToInt32(txtPatientId.Text));
  
  _patient.SSN = txtSSN.Text;
  _patient.FirstName = txtPatientFirstName.Text;
  _patient.LastName = txtPatientLastName.Text;
  _patient.Middle = txtPatientMiddle.Text;
  _patient.AddressOne = txtPatientAddressOne.Text;
  _patient.City = txtPatientCity.Text;
  _patient.State = txtPatientState.Text;
  _patient.ZipCode = txtPatientZip.Text;
  
  _patient.HomePhone = txtPatientHomePhone.Text;
  _patient.WorkPhone = txtPatientWorkPhone.Text;
  _patient.CellPhone = txtPatientCellPhone.Text;
  
  if (rBtnHomePhone.Checked == true)
      //   _patient.ApptPhone = txtPatientHomePhone.Text;
  
  if (rBtnHomePhone.Checked == true)
      //   _patient.ApptPhone = txtPatientHomePhone.Text;
  
  if (rBtnWorkPhone.Checked == true)
      // _patient.ApptPhone = txtPatientWorkPhone.Text;
  
  _patient.BirthDate = dtBirthDate.DateTime;
  _patient.emailAddress = txtPatientEmail.Text;
  _patient.Race =   Convert.ToInt32(dpRace.SelectedValue);
  _patient.Ethnicity =Convert.ToInt32(dpEthnicity.SelectedValue);
  _patient.Language =  Convert.ToInt32(dpLanguages.SelectedValue);
  
  if (dpGender.Text == "")
  {
      dpGender.Focus();
      errorCount = 1;
      lblGenderRequired.Text = "* Gender is required.";
  }
  else
  {
      errorCount = 0;
      lblGenderRequired.Visible = false;
  }
  
  _patient.Gender = "M";
  _patient.PatientID = txtPatientId.Text;
  
  SourceDal.SourceEntities.Patients.Attach(_patient);
  SourceDal.SourceEntities.Patients.Context.ObjectStateManager.ChangeObjectState(_patient, EntityState.Modified);
  SourceDal.SourceEntities.SaveChanges();
  </code></pre>
  
  <p>The error I get is</p>
  
  <blockquote>
    <p>An unhandled exception of type 'System.InvalidOperationException' occurred in System.Data.Entity.dll</p>
    
    <p>Additional information: An entity object cannot be referenced by multiple instances of IEntityChangeTracker.</p>
  </blockquote>
  
  <p><strong>Edit 2</strong>:</p>
  
  <p>Code to show my function <code>getPaitnetByPatineyNewId</code></p>
  
  <pre><code>    public Patient getPatientByPatientNewId(int newId)
      {
          Patient patient = new Patient();
  
          if (newId == -1)
          {
              patient = new Patient();
          }
          else
          {
              patient = SourceEntities.Patients
                                      .Where(w =&gt; w.ID == newId)
                                      .FirstOrDefault();
          }
  
          return patient;
      }
  </code></pre>