public ActionResult Delete(int id)
        {
            if (!AuthenticationController.IsUserAuthorized(HttpContext, AuthenticationController.UserRole.Admin))
            {
                return(RedirectToAction("NotAuthorized", "Authentication"));
            }

            var theses = _context.Theses
                         .Where(t => t.SuperId == id)
                         .ToList();

            foreach (var thesis in theses)
            {
                _context.Entry(thesis).State = EntityState.Deleted;
            }

            var supervisor = _context.Supervisors
                             .FirstOrDefault(s => s.Id == id);
            var user = _context.Users
                       .FirstOrDefault(u => u.Id == supervisor.UserId);

            _context.Entry(supervisor).State = EntityState.Deleted;
            _context.Entry(user).State       = EntityState.Deleted;
            _context.SaveChanges();

            return(RedirectToAction(nameof(Index)));
        }
示例#2
0
        public ActionResult Delete(int id)
        {
            if (!AuthenticationController.IsUserAuthorized(HttpContext, AuthenticationController.UserRole.Admin))
            {
                return(RedirectToAction("NotAuthorized", "Authentication"));
            }

            var student = _context.Students
                          .FirstOrDefault(s => s.Id == id);
            var user = new User {
                Id = student.UserId
            };

            using (var transaction = _context.Database.BeginTransaction())
            {
                var studentThesis = _context.Theses
                                    .FirstOrDefault(t => t.StudentId == student.Id);
                if (studentThesis != null)
                {
                    studentThesis.StudentId = null;
                    _context.SaveChanges();
                }

                _context.Remove(student);
                _context.SaveChanges();

                _context.Entry(user).State = EntityState.Deleted;
                _context.SaveChanges();

                transaction.Commit();
            }
            return(RedirectToAction(nameof(Index)));
        }