コード例 #1
0
        public IActionResult Delete(int id)
        {
            var student = schoolDbContext.Students.Find(id);

            schoolDbContext.Remove <Student>(student);
            schoolDbContext.SaveChanges();
            return(RedirectToAction("Index"));
        }
コード例 #2
0
ファイル: EntityStatesTest.cs プロジェクト: xmicha82/ICS
 public void DeletedStateTest()
 {
     _schoolDbContextSut.Students.Add(_studentEntity);
     _schoolDbContextSut.SaveChanges();
     _schoolDbContextSut.Remove(_studentEntity);
     Assert.Equal(EntityState.Deleted, _schoolDbContextSut.Entry(_studentEntity).State);
 }
コード例 #3
0
        /// <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);
                    }
                }
            }
        }
コード例 #4
0
 public void DeletedStateTest()
 {
     schoolDbContextSUT.Students.Add(studentEntity);
     schoolDbContextSUT.SaveChanges();
     schoolDbContextSUT.Remove(studentEntity);
     Assert.Equal(EntityState.Deleted, schoolDbContextSUT.Entry(studentEntity).State);
 }
コード例 #5
0
        public IActionResult RemovePerson(int id)
        {
            Person foundPerson = _context.Person.Find(id);

            _context.Remove(foundPerson);
            _context.SaveChanges();
            return(RedirectToAction("ListPeople"));
        }
コード例 #6
0
        public string DeleteStudent(int id)
        {
            var student = schoolDbContext.Students.Find(id);

            schoolDbContext.Remove <Student>(student);
            schoolDbContext.SaveChanges();
            return("Deleted Successfully!");
        }
コード例 #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()));
        }
コード例 #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 }));
        }
コード例 #9
0
        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();
            }
        }
コード例 #10
0
 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);
     }
 }
コード例 #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);
                    }
                }
            }
        }
コード例 #12
0
 public void Remove(CourseAssignement c)
 {
     context.Remove(c);
 }
コード例 #13
0
 public void Remove(ClassAssignement c)
 {
     context.Remove(c);
 }
コード例 #14
0
 public void Remove(Student c)
 {
     context.Remove(c);
 }
コード例 #15
0
 public void Remove(Course c)
 {
     context.Remove(c);
 }