public async Task <IEnumerable <Guid> > Update(UserModel user, params PersonViewModel[] models) { var recordsToUpdate = new List <PersonModel>(); var now = DateTime.UtcNow; foreach (var model in models) { var currentRecord = await _personDataService.Get(model.Id); var record = currentRecord.First(); record.Name = model.Name; record.DocumentCode = model.DocumentCode; record.DocumentType = model.DocumentType; record.PreferedLanguage = model.PreferedLanguage; record.IsActive = model.IsActive; foreach (var item in model.Addresses) { if ((item.Id ?? Guid.Empty) == Guid.Empty) { item.Id = Guid.NewGuid(); } var current = record.Addresses.SingleOrDefault(x => x.Id == item.Id); if (current == null) { var newGuid = Guid.NewGuid(); record.Addresses.Add(new AddressModel { Id = current.Id, Street = current.Street, Number = current.Number, ComplementaryInformation = current.ComplementaryInformation, City = current.City, Province = current.Province, Country = current.Country, ZipCode = current.ZipCode, Longitude = current.Longitude, Latitude = current.Latitude, IncludeDate = now, IsActive = true }); } else { current.Id = current.Id; current.Street = current.Street; current.Number = current.Number; current.ComplementaryInformation = current.ComplementaryInformation; current.City = current.City; current.Province = current.Province; current.Country = current.Country; current.ZipCode = current.ZipCode; current.Longitude = current.Longitude; current.Latitude = current.Latitude; current.UpdateDate = now; } } foreach (var item in model.Contacts) { if ((item.Id ?? Guid.Empty) == Guid.Empty) { item.Id = Guid.NewGuid(); } var current = record.Contacts.SingleOrDefault(x => x.Id == item.Id); if (current == null) { var newGuid = Guid.NewGuid(); record.Contacts.Add(new ContactModel { Id = item.Id.Value, Type = item.Type, Comment = item.Comment, Value = item.Value, IncludeDate = now, IsActive = true }); } else { current.Type = item.Type; current.Comment = item.Comment; current.Value = item.Value; current.UpdateDate = now; } } foreach (var item in model.Notes) { if ((item.Id ?? Guid.Empty) == Guid.Empty) { item.Id = Guid.NewGuid(); } var current = record.Notes.SingleOrDefault(x => x.Id == item.Id); if (current == null) { var newGuid = Guid.NewGuid(); record.Notes.Add(new NoteModel { Id = item.Id.Value, Note = item.Text, UpdateDate = now }); } else { current.Note = item.Text; current.IncludeDate = now; } } } return(await _personDataService.Update(recordsToUpdate)); }