public async Task UpdateAsync(Exame exame) { var entityExist = await GetByIdAsync(exame.Id); if (entityExist == null) { return; } _context.Entry(entityExist).State = EntityState.Modified; _context.Entry(entityExist).CurrentValues.SetValues(exame); }
public async Task UpdateAsync(OrdemServico ordemServico) { var entityExist = await GetByIdAsync(ordemServico.Id); if (entityExist == null) { return; } _context.Entry(entityExist).State = EntityState.Modified; _context.Entry(entityExist).CurrentValues.SetValues(ordemServico); foreach (var exame in entityExist.Exames.ToList()) { if (!ordemServico.Exames.Any(x => x.Id == exame.Id)) { _context.Entry(exame).State = EntityState.Deleted; } } ; var newOrdemServicoExameList = new List <OrdemServicoExame>(); foreach (var exame in ordemServico.Exames) { var entityChildExist = entityExist.Exames.FirstOrDefault(x => x.Id == exame.Id); if (entityChildExist == null) { newOrdemServicoExameList.Add(exame); } else { _context.Entry(entityChildExist).State = EntityState.Modified; _context.Entry(entityChildExist).CurrentValues.SetValues(exame); } } ; await _context.OrdemServicoExames.AddRangeAsync(newOrdemServicoExameList); }