title: How to update data in entity framework using automapper?
body: <p>I have this code but I can't get it to work together, if I comment the first 4 lines of code if I update the address,and if I comment on the code to update the address, it no longer updates the  ClienteTipodeConcepto.</p>
  <pre><code>[HttpPut]
  public async Task&lt;ActionResult&gt; Put(Cliente cliente)
  {
  
      var clienteDB = await context.Clientes.FirstOrDefaultAsync(x =&gt; x.ClienteId == cliente.ClienteId);
      if (clienteDB == null) { return NotFound(); }
      clienteDB = mapper.Map(cliente, clienteDB);
      clienteDB.ClienteTipodeConcepto = cliente.ClienteTipodeConcepto;
  
      context.Entry(cliente).State = EntityState.Modified;
      foreach (var direccion in cliente.Direccion)
      {
          if (direccion.DireccionId != null)
          {
              context.Entry(direccion).State = EntityState.Modified;
          }
          else
          {
              context.Entry(direccion).State = EntityState.Added;
          }
      }
  
      var listadoDireccionesIds = cliente.Direccion.Select(x =&gt; x.DireccionId).ToList();
      var direccionesABorrar = await context
          .Direcciones
          .Where(x =&gt; !listadoDireccionesIds.Contains(x.DireccionId) &amp;&amp; x.Cliente.ClienteId == cliente.ClienteId)
          .ToListAsync();
      context.RemoveRange(direccionesABorrar);           
  
      await context.SaveChangesAsync();
      return NoContent();
  }
  </code></pre>
  <p>this part:</p>
  <pre><code>  var clienteDB = await context.Clientes.FirstOrDefaultAsync(x =&gt; x.ClienteId == cliente.ClienteId);
      if (clienteDB == null) { return NotFound(); }
      clienteDB = mapper.Map(cliente, clienteDB);
      clienteDB.ClienteTipodeConcepto = cliente.ClienteTipodeConcepto;
  </code></pre>
  <p>no longer allows me to update what is below:</p>
  <pre><code> context.Entry(cliente).State = EntityState.Modified;
      foreach (var direccion in cliente.Direccion)
      {
          if (direccion.DireccionId != null)
          {
              context.Entry(direccion).State = EntityState.Modified;
          }
          else
          {
              context.Entry(direccion).State = EntityState.Added;
          }
      }
  </code></pre>
  <p>error:</p>
  <blockquote>
  <p>Error AutoMapper.AutoMapperMappingException: Error mapping types.
  Mapping types: Cliente -&gt; Cliente BlazorApp.Shared.Models.Cliente -&gt;
  BlazorApp.Shared.Models.Cliente Type Map configuration: Cliente -&gt;
  Cliente BlazorApp.Shared.Models.Cliente -&gt;
  BlazorApp.Shared.Models.Cliente Destination Member:
  ClienteTipodeConcepto ---&gt; System.InvalidOperationException: The
  instance of entity type 'Direccion' cannot be tracked because another
  instance with the same key value for {'DireccionId'} is already being
  tracked. When attaching existing entities, ensure that only one entity
  instance with a given key value is attached. Consider using</p>
  </blockquote>
  <p>How can I make the whole form update fine?</p>