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.
[HttpPut]
public async Task<ActionResult> Put(Cliente cliente)
{
var clienteDB = await context.Clientes.FirstOrDefaultAsync(x => 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 => x.DireccionId).ToList();
var direccionesABorrar = await context
.Direcciones
.Where(x => !listadoDireccionesIds.Contains(x.DireccionId) && x.Cliente.ClienteId == cliente.ClienteId)
.ToListAsync();
context.RemoveRange(direccionesABorrar);
await context.SaveChangesAsync();
return NoContent();
}
this part:
var clienteDB = await context.Clientes.FirstOrDefaultAsync(x => x.ClienteId == cliente.ClienteId);
if (clienteDB == null) { return NotFound(); }
clienteDB = mapper.Map(cliente, clienteDB);
clienteDB.ClienteTipodeConcepto = cliente.ClienteTipodeConcepto;
no longer allows me to update what is below:
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;
}
}
error:
Error AutoMapper.AutoMapperMappingException: Error mapping types. Mapping types: Cliente -> Cliente BlazorApp.Shared.Models.Cliente -> BlazorApp.Shared.Models.Cliente Type Map configuration: Cliente -> Cliente BlazorApp.Shared.Models.Cliente -> BlazorApp.Shared.Models.Cliente Destination Member: ClienteTipodeConcepto ---> 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
How can I make the whole form update fine?