public async Task UploadFileToDocument(UploadAttachmentModel upload) { using (var unitOfWork = await DataConnectionFactory.CreateUnitOfWork()) { var document = await unitOfWork.Documents.GetById(upload.DocumentId); if (document == null) { throw new NotFoundException("Document not found."); } if (document.FileId.HasValue) { throw new LogicException("A file is already attached to this document."); } var file = await _fileProvider.SaveFile(upload); document.Attachment = file; await unitOfWork.Documents.Update(document); await unitOfWork.SaveChangesAsync(); } }