public IActionResult Delete(int id)
        {
            var student = schoolDbContext.Students.Find(id);

            schoolDbContext.Remove <Student>(student);
            schoolDbContext.SaveChanges();
            return(RedirectToAction("Index"));
        }
Exemple #2
0
 public void DeletedStateTest()
 {
     _schoolDbContextSut.Students.Add(_studentEntity);
     _schoolDbContextSut.SaveChanges();
     _schoolDbContextSut.Remove(_studentEntity);
     Assert.Equal(EntityState.Deleted, _schoolDbContextSut.Entry(_studentEntity).State);
 }
        /// <summary>
        /// 更改老师得授课信息
        /// </summary>
        /// <param name="selectedCourse">选中得课程信息</param>
        /// <param name="instructorToUpdate">需要修改得教师</param>
        private void UpdateInstructorCourse(string[] selectedCourse, Instructor instructorToUpdate)
        {
            if (selectedCourse == null)
            {
                instructorToUpdate.CourseAssignments = new List <CourseAssignment>();
                return;
            }
            var selectedCoursesHS = new HashSet <string>(selectedCourse);

            var instructorCourse = new HashSet <int>(instructorToUpdate.CourseAssignments.Select(a => a.CourseId));

            foreach (var course in _context.Courses)
            {
                if (selectedCoursesHS.Contains(course.CourseId.ToString()))
                {
                    if (!instructorCourse.Contains(course.CourseId))
                    {
                        instructorToUpdate.CourseAssignments.Add(
                            new CourseAssignment()
                        {
                            InstructorId = instructorToUpdate.Id,
                            CourseId     = course.CourseId
                        });
                    }
                }
                else
                {
                    if (instructorCourse.Contains(course.CourseId))
                    {
                        var courseToRemove = instructorToUpdate.CourseAssignments.SingleOrDefault(a => a.CourseId == course.CourseId);
                        _context.Remove(courseToRemove);
                    }
                }
            }
        }
Exemple #4
0
 public void DeletedStateTest()
 {
     schoolDbContextSUT.Students.Add(studentEntity);
     schoolDbContextSUT.SaveChanges();
     schoolDbContextSUT.Remove(studentEntity);
     Assert.Equal(EntityState.Deleted, schoolDbContextSUT.Entry(studentEntity).State);
 }
Exemple #5
0
        public IActionResult RemovePerson(int id)
        {
            Person foundPerson = _context.Person.Find(id);

            _context.Remove(foundPerson);
            _context.SaveChanges();
            return(RedirectToAction("ListPeople"));
        }
        public string DeleteStudent(int id)
        {
            var student = schoolDbContext.Students.Find(id);

            schoolDbContext.Remove <Student>(student);
            schoolDbContext.SaveChanges();
            return("Deleted Successfully!");
        }
Exemple #7
0
        public async Task <IdentityResult> DeleteAsync(Users user, CancellationToken cancellationToken)
        {
            db.Remove(user);

            int i = await db.SaveChangesAsync(cancellationToken);

            return(await Task.FromResult(i == 1?IdentityResult.Success : IdentityResult.Failed()));
        }
Exemple #8
0
        public async Task <IActionResult> RemoveStudent(int id, int secondaryId)
        {
            var courseId  = id;
            var studentId = secondaryId;
            var course    = await courseService.GetAsync(id);

            var student = await studentService.GetAsync(studentId);

            if (course != null && student != null)
            {
                var preexisting = ctx.Enrollments
                                  .Where(e => e.CourseId == id && e.StudentId == studentId)
                                  .FirstOrDefault();
                if (preexisting != null)
                {
                    ctx.Remove(preexisting);
                    await ctx.SaveChangesAsync();
                }
            }
            return(RedirectToAction("View", new { Id = id }));
        }
        public void LazyEvaluationTest()
        {
            IEnumerable <StudentEntity> students;

            using (var schoolDbContextSUT = new SchoolDbContext())
            {
                schoolDbContextSUT.Students.Add(studentEntity);
                schoolDbContextSUT.SaveChanges();

                students = schoolDbContextSUT.Students.Where(s => s.Id == studentEntity.Id);
            }

            //Materialized outside of using scope
            Assert.Throws <ObjectDisposedException>(() => students.ToList());

            using (var schoolDbContextSUT = new SchoolDbContext())
            {
                schoolDbContextSUT.Remove(studentEntity);
                schoolDbContextSUT.SaveChanges();
            }
        }
 public virtual void Delete(T entity)
 {
     try
     {
         dbContext.Remove <T>(entity);
     }
     catch (DbUpdateConcurrencyException e)
     {
         var errorMessage = ProcessException("Delete db Update concurrency error: ",
                                             e.Message,
                                             e.InnerException,
                                             ErrorType.Critical);
         throw new DataAccessException(errorMessage);
     }
     catch (DbUpdateException e)
     {
         var errorMessage = ProcessException("Delete db update error: ",
                                             e.Message,
                                             e.InnerException,
                                             ErrorType.Critical);
         throw new DataAccessException(errorMessage);
     }
     catch (InvalidOperationException e)
     {
         var errorMessage = ProcessException("Delete db invalid operation error: ",
                                             e.Message,
                                             e.InnerException,
                                             ErrorType.Critical);
         throw new DataAccessException(errorMessage);
     }
     catch (Exception e)
     {
         var errorMessage = ProcessException("Delete db general error: ",
                                             e.Message,
                                             e.InnerException,
                                             ErrorType.Critical);
         throw new DataAccessException(errorMessage);
     }
 }
Exemple #11
0
        private void UpdateInstructorCourses(IEnumerable <int> coursesAssignedNew, Instructor instructorToUpdate)
        {
            if (coursesAssignedNew == null || !coursesAssignedNew.Any())
            {
                instructorToUpdate.CourseAssignments = new List <CourseAssignment>();
                return;
            }

            var coursesAssignedExisting = new HashSet <int>(
                instructorToUpdate.CourseAssignments.Select(ca => ca.CourseId));

            foreach (var course in _context.Courses)
            {
                if (coursesAssignedNew.Contains(course.CourseId))
                {
                    if (!coursesAssignedExisting.Contains(course.CourseId))
                    {
                        // add new record
                        instructorToUpdate.CourseAssignments.Add(
                            new CourseAssignment
                        {
                            InstructorId = instructorToUpdate.InstructorId,
                            CourseId     = course.CourseId
                        });
                    }
                }
                else
                {
                    if (coursesAssignedExisting.Contains(course.CourseId))
                    {
                        // remove existing record
                        var itemToRemove = instructorToUpdate.CourseAssignments.FirstOrDefault(ca => ca.CourseId == course.CourseId);
                        _context.Remove(itemToRemove);
                    }
                }
            }
        }
 public void Remove(CourseAssignement c)
 {
     context.Remove(c);
 }
Exemple #13
0
 public void Remove(ClassAssignement c)
 {
     context.Remove(c);
 }
Exemple #14
0
 public void Remove(Student c)
 {
     context.Remove(c);
 }
Exemple #15
0
 public void Remove(Course c)
 {
     context.Remove(c);
 }