/// <summary>
        /// Update entity in contacts
        /// <param name="body"></param>
        /// <param name="requestConfiguration">Configuration for the request such as headers, query parameters, and middleware options.</param>
        /// </summary>
        public RequestInformation CreatePatchRequestInformation(OrgContact body, Action <OrgContactItemRequestBuilderPatchRequestConfiguration> requestConfiguration = default)
        {
            _ = body ?? throw new ArgumentNullException(nameof(body));
            var requestInfo = new RequestInformation {
                HttpMethod     = Method.PATCH,
                UrlTemplate    = UrlTemplate,
                PathParameters = PathParameters,
            };

            requestInfo.SetContentFromParsable(RequestAdapter, "application/json", body);
            if (requestConfiguration != null)
            {
                var requestConfig = new OrgContactItemRequestBuilderPatchRequestConfiguration();
                requestConfiguration.Invoke(requestConfig);
                requestInfo.AddRequestOptions(requestConfig.Options);
                requestInfo.AddHeaders(requestConfig.Headers);
            }
            return(requestInfo);
        }
        public async Task UpdateAsync(Guid id, OrganizationDto model)
        {
            if (id != model.Id)
            {
                throw new BadRequestException("Org id mismatch");
            }

            Organization org = _mapper.Map <Organization>(model);

            Organization dbOrg = await _context.Organizations
                                 .Where(o => o.Id == org.Id)
                                 .Include(o => o.OrgContacts).ThenInclude(oc => oc.Contact)
                                 .Include(o => o.OrgAddresses).ThenInclude(oa => oa.Address)
                                 .FirstOrDefaultAsync();

            if (dbOrg == null)
            {
                //org.DateCreated = DateTime.UtcNow;
                //org.DateModified = null;
                //await _context.Organizations.AddAsync(org);
                throw new NotFoundException("Organization not found");
            }

            // delete all contacts that are no longer exists
            foreach (OrgContact dbOrgContact in dbOrg.OrgContacts)
            {
                Contact dbContact = dbOrgContact.Contact;
                if (org.OrgContacts.All(oc => oc.Contact.Id != dbContact.Id))
                {
                    _context.Contacts.Remove(dbContact);
                }
            }
            // delete all addresses that are no longer exists
            foreach (OrgAddress dbOrgAddress in dbOrg.OrgAddresses)
            {
                Address dbAddress = dbOrgAddress.Address;
                if (org.OrgAddresses.All(oa => oa.Address.Id != dbAddress.Id))
                {
                    _context.Addresses.Remove(dbAddress);
                }
            }
            // copy current (incoming) values to db
            org.DateModified = DateTime.UtcNow;
            _context.Entry(dbOrg).CurrentValues.SetValues(org);

            #region Contacts
            var contactPairs = from curr in org.OrgContacts.Select(oc => oc.Contact)
                               join db in dbOrg.OrgContacts.Select(oc => oc.Contact)
                               on curr.Id equals db.Id into grp
                               from db in grp.DefaultIfEmpty()
                               select new { curr, db };
            foreach (var pair in contactPairs)
            {
                if (pair.db != null)
                {
                    _context.Entry(pair.db).CurrentValues.SetValues(pair.curr);
                    _context.Contacts.Update(pair.db);
                }
                else
                {
                    var orgContact = new OrgContact
                    {
                        OrgId        = org.Id,
                        Organization = org,
                        ContactId    = pair.curr.Id,
                        Contact      = pair.curr
                    };
                    dbOrg.OrgContacts.Add(orgContact);
                }
            }
            #endregion

            #region Addresses
            var addressPairs = from curr in org.OrgAddresses.Select(oa => oa.Address)
                               join db in dbOrg.OrgAddresses.Select(oa => oa.Address)
                               on curr.Id equals db.Id into grp
                               from db in grp.DefaultIfEmpty()
                               select new { curr, db };
            foreach (var pair in addressPairs)
            {
                if (pair.db != null)
                {
                    _context.Entry(pair.db).CurrentValues.SetValues(pair.curr);
                    _context.Addresses.Update(pair.db);
                }
                else
                {
                    var orgAddress = new OrgAddress
                    {
                        OrgId        = org.Id,
                        Organization = org,
                        AddressId    = pair.curr.Id,
                        Address      = pair.curr
                    };
                    dbOrg.OrgAddresses.Add(orgAddress);
                }
            }

            _context.Organizations.Update(dbOrg);
            #endregion

            await _context.SaveChangesAsync();

            //model = _mapper.Map<OrganizationDto>(org);
            //return model;
        }