internal async Task <Restroom> SaveRestroomAsync(Restroom model) { Entity.Restroom restroom = null; if (model.RestroomId > 0) { var r = await RestroomUnitOfWork.GetRestroomAsync(model.RestroomId); restroom = model.ToDataAccessFrom(r); Logger.WriteDebug("SaveRestroomAsync->After GetRestroomAsync and ToDataAccessFrom:" + JsonConvert.SerializeObject(restroom)); } else { restroom = model.ToDataAccess(); restroom.UpdUserId = CurrentUserName; } var hasContact = false; var contactChanged = false; hasContact = !string.IsNullOrWhiteSpace(model.ContactName) || !string.IsNullOrWhiteSpace(model.ContactTitle) || !string.IsNullOrWhiteSpace(model.ContactEmail) || !string.IsNullOrWhiteSpace(model.ContactPhone); Entity.Contact contact = null; if (!hasContact && restroom.ContactId.HasValue && restroom.ContactId > 0) { restroom.ContactId = null; restroom.Contact = null; } else if (restroom.ContactId.HasValue && restroom.ContactId > 0) { contact = await RestroomUnitOfWork.GetByIdAsync <Entity.Contact, int>(restroom.ContactId.Value); if (contact.ContactName != model.ContactName || contact.Title != model.ContactTitle || contact.Email != model.ContactEmail || contact.Phone != model.ContactPhone || contact.ServiceProvider != model.ServiceProvider ) { contactChanged = true; } } if (contactChanged || (hasContact && (!restroom.ContactId.HasValue || restroom.ContactId == 0))) { contact = new Entity.Contact { Title = model.ContactTitle, Email = model.ContactEmail, Phone = model.ContactPhone, ContactName = model.ContactName, ServiceProvider = model.ServiceProvider }; contact = RestroomUnitOfWork.Create(contact); restroom.Contact = contact; } //restroom.Active = true; restroom.StatusListId = 1; // always pending... try { var savedModel = await RestroomUnitOfWork.SaveRestroomAsync(restroom); return(savedModel == null ? null : Restroom.FromDataAccess(savedModel)); } catch (DbEntityValidationException ex) { var errTxt = ex.GetStringRepresentation(); Logger.WriteError("RestroomHandler.SaveRestroomAsync.DbEntityValidationException -> EntityValidationErrors : \r\n" + errTxt); throw; } }