title: I can't update a foreign key and navigation property with entity-framework
body: <p>I am building an application using .net 5 and entity framework core 5.0 with lazy loading. I am trying to update a &quot;Candidato&quot; entity that is a child of a &quot;Puesto&quot; entity.</p>
  <pre><code>public class Candidato : EntidadBase
  {
      [Key]
      public int Code { get; set; }
  
      public int PuestoId { get; set; }
  
      public string NombrePartido { get; set; }
  
    
      [JsonIgnore]
      public virtual Puesto Puesto { get; set; }
  
  }
  
  public class Puesto : EntidadBase
      {
          [Key]
          public int Code { get; set; }
  
          public int PeriodoId { get; set; }
  
          public string Nombre { get; set; }
  
   
          [JsonIgnore]
          public virtual Periodo Periodo { get; set; }
  }
  </code></pre>
  <p>As I only want to make an update of some specific fields, first I obtain the current one from the database, to this I assign the properties that interest me, including the foreign key &quot;PuestoId&quot;, and I save this same entity as follows :</p>
  <pre><code>        Candidato actual = CandidatosDAO.GetCandidato(candidato.Code); //actual.PuestoID = 3 ; actual.Puesto.Code = 3
  
          actual.PuestoId = candidato.PuestoId; // actual.PuestoId = candidato.PuestoId = 1; actual.Puesto.Code = 3
          actual.NombrePartido = candidato.NombrePartido;
  
          CandidatosDAO.ActualizarCandidato(actual); //After performing update &quot;actual&quot; returned to actual.PuestoID = 3
  
  public static Candidato GetCandidato(int canCode)
  {
      ContextDB db = new ContextDB();
  
      return db.Candidatos.Where(m =&gt; m.Code == canCode).FirstOrDefault();
  }
  
  internal static void ActualizarCandidato(Candidato candidato)
  {
      candidato.UpdateDate = DateTime.Now;
      ContextDB db = new ContextDB();
  
      db.Candidatos.Update(candidato);
      db.SaveChanges();
  }
  </code></pre>
  <p>My problem is that when i assign the new &quot;PuestoId&quot; to &quot;actual&quot;, the navigation property &quot;Puesto&quot; is displayed with the old one related entity, and after performing &quot;UpdateCandidate&quot;, the &quot;PositionID&quot; of &quot;actual&quot; returns to its old value, ignoring updating it.</p>
  <p>The only way I found to get it to update correctly was by changing &quot;Candidatos.Update(candidato)&quot; to &quot;db.Entry (candidate) .State = EntityState.Modified&quot;, but the related entity &quot;Puesto&quot; is yet in the old one.</p>