public async Task DeleteCandidate(long candidateId) { var skills = WorkScope.GetAll <CVSkills>().Where(c => c.CvcandidateId == candidateId); foreach (var skill in skills) { await WorkScope.GetRepo <CVSkills, long>().DeleteAsync(skill); } var candidate = await WorkScope.GetAll <CVCandidates>().FirstOrDefaultAsync(c => c.Id == candidateId); if (candidate == default) { throw new UserFriendlyException(ErrorCodes.NotFound.Candidate); } var pathRoot = _hostingEnvironment.WebRootPath; if (candidate.AttachmentPatch != null) { var docPath = Path.Combine(pathRoot, candidate.AttachmentPatch.Replace("/", @"\")); if (File.Exists(docPath)) { File.Delete(docPath); } } await WorkScope.Repository <CVCandidates>().DeleteAsync(candidate); var attachments = WorkScope.GetAll <CVAttachments>().Where(c => c.CvcandidateId == candidateId); foreach (var att in attachments) { await WorkScope.GetRepo <CVAttachments, long>().DeleteAsync(att); } var interviewers = WorkScope.GetAll <InterviewCandidates>().Where(c => c.CvcandidateId == candidateId); foreach (var inter in interviewers) { await WorkScope.GetRepo <InterviewCandidates, long>().DeleteAsync(inter); } foreach (var att in attachments) { var file = Path.Combine(pathRoot, att.Path.Replace("/", @"\")); if (File.Exists(file)) { File.Delete(file); } else { continue; } } }