Beispiel #1
0
        public static void Main()
        {
            Database.SetInitializer(new MigrateDatabaseToLatestVersion<UniversityDbContext, Configuration>());

            var db = new UniversityDbContext();

            var student = new Student
            {
                Name = "Pesho",
                FN = "M12345",
                Gender = Gender.Male
            };

            db.Students.Add(student);

            //delete student Pesho
            //var studentsForDelete = db.Students.Where(s => s.Name == "Pesho").ToList();
            //foreach (var item in studentsForDelete)
            //{
            //    db.Students.Remove(item);
            //}

            db.SaveChanges();
           
            Console.WriteLine("Students count: {0}", db.Students.Count());
            Console.WriteLine("Courses count: {0}", db.Courses.Count());
            Console.WriteLine("Authors count: {0}", db.Authors.Count());
            Console.WriteLine("Materials count: {0}", db.Materials.Count());
        }
Beispiel #2
0
        public void DeberiActualizar()
        {
            var mockSet = new Mock<DbSet<Student>>();
            var mockContext = new Mock<UniversityContext>();
            mockContext.Setup(m => m.Students).Returns(mockSet.Object);
            var existing = new Student { StudentId = 1, Nombre = "Carlos", Paterno = "Estrada", Materno = "Copa", Telefono = 2269587 };
            var data = new Student
            {
                StudentId = 1,
                Nombre = "Carlos",
                Paterno = "Estrada",
                Materno = "Copa",
                Telefono = 2269587,
            };
            var queryable = new List<Student> { data }.AsQueryable();
            mockSet.As<IQueryable<Student>>().Setup(m => m.Provider).Returns(queryable.Provider);
            mockSet.As<IQueryable<Student>>().Setup(m => m.Expression).Returns(queryable.Expression);
            mockSet.As<IQueryable<Student>>().Setup(m => m.ElementType).Returns(queryable.ElementType);
            mockSet.As<IQueryable<Student>>().Setup(m => m.GetEnumerator()).Returns(queryable.GetEnumerator());
            mockContext.Setup(m => m.SaveChanges()).Verifiable();

            var service = new UniversityContextService(mockContext.Object);
            service.UpdateStudent(data);
            var mostrar = service.Show();

            mockContext.Verify();
            Assert.AreEqual(1, mostrar.Count);
            Assert.AreEqual("Carlos", mostrar[0].Nombre);
            Assert.AreEqual(2269587, mostrar[0].Telefono);
        }
Beispiel #3
0
        private static void AddStudent(string fName, string lName, string number, Course course)
        {
            var db = new UniversityContext();

            var student = new Student();

            student.FirstName = fName;
            student.LastName = lName;
            student.Number = number;
            student.Courses.Add(course);

            db.Students.Add(student);
            db.SaveChanges();
        }
        public async Task <IActionResult> OnGetAsync(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            Student = await _context.Students.FirstOrDefaultAsync(m => m.ID == id);

            if (Student == null)
            {
                return(NotFound());
            }
            return(Page());
        }
 public void UpdateStudent(Student student)
 {
     int? x = student.StudentId;
     if (!x.HasValue)
     {
         _context.Students.Add(new Student { StudentId = student.StudentId, Nombre = student.Nombre, Paterno = student.Paterno, Materno = student.Materno, Telefono = student.Telefono});
         _context.SaveChanges();
     }
     else
     {
         var entity = _context.Students.Where(s => s.StudentId == student.StudentId).Single();
         entity.Telefono = student.Telefono;
         _context.SaveChanges();
     }
 }
        public async Task <IActionResult> OnPostAsync(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            Student = await _context.Students.FindAsync(id);

            if (Student != null)
            {
                _context.Students.Remove(Student);
                await _context.SaveChangesAsync();
            }

            return(RedirectToPage("./Index"));
        }
Beispiel #7
0
 public void DeberiaEliminar()
 {
     var mockSet = new Mock<DbSet<Student>>();
     var mockContext = new Mock<UniversityContext>();
     mockContext.Setup(m => m.Students).Returns(mockSet.Object);
     var data = new Student { StudentId = 1, Nombre = "Carlos", Paterno = "Estrada", Materno = "Copa", Telefono = 2269587};
     var queryable = new List<Student> { data }.AsQueryable();
     mockSet.As<IQueryable<Student>>().Setup(m => m.Provider).Returns(queryable.Provider);
     mockSet.As<IQueryable<Student>>().Setup(m => m.Expression).Returns(queryable.Expression);
     mockSet.As<IQueryable<Student>>().Setup(m => m.ElementType).Returns(queryable.ElementType);
     mockSet.As<IQueryable<Student>>().Setup(m => m.GetEnumerator()).Returns(queryable.GetEnumerator());
     mockSet.Setup(m => m.Remove(data)).Verifiable();
     mockContext.Setup(m => m.SaveChanges()).Verifiable();
     var service = new UniversityContextService(mockContext.Object);
     service.DeleteStudent("Carlos");//elimina por nombre
     mockContext.Verify();
 }
Beispiel #8
0
        static void Main(string[] args)
        {
            Database.SetInitializer(new MigrateDatabaseToLatestVersion<UniversityContext, Configuration>());

            var db = new UniversityContext();
            var student = new Student { Name = "Pesho", Number = 11111 };
            var studentTwo = new Student { Name = "Misho", Number = 22222 };
            var studentThree = new Student { Name = "Gosho", Number = 33333 };
            var course = new Course { Name = "Programming 101", Description = "Introduction to Programming", Materials = "Youtube videos" };
            student.Homeworks.Add(new Homework { Student = student, Content = "Some content" });
            course.Homeworks.Add(new Homework { Course = course, Content = "Some content" });
            db.Students.Add(student);
            db.Students.Add(studentTwo);
            db.Students.Add(studentThree);
            db.Courses.Add(course);
            db.SaveChanges();
        }
 public void UpdateStudent(Student student)
 {
     context.Entry(student).State = EntityState.Modified;
 }
 public void InsertStudent(Student student)
 {
     context.Students.Add(student);
 }
Beispiel #11
0
        private void UpdateStudentCourses(string[] selectedCourses, Student studentToUpdate)
        {
            if (selectedCourses == null)
            {
                studentToUpdate.Enrollments = new List<Enrollment>();
                return;
            }

            var selectedCoursesHS = new HashSet<string>(selectedCourses);
            var studentCourses = new HashSet<int>
            (studentToUpdate.Enrollments.Select(c => c.CourseID));

            foreach (var enrollment in db.Enrollments)
            {
                if (selectedCoursesHS.Contains(enrollment.CourseID.ToString()))
                {
                    if (!studentCourses.Contains(enrollment.CourseID))
                    {
                        studentToUpdate.Enrollments.Add(enrollment);
                    }
                }
                else
                {
                    if (studentCourses.Contains(enrollment.CourseID))
                    {
                        studentToUpdate.Enrollments.Remove(enrollment);
                    }
                }
            }
        }
Beispiel #12
0
 private void PopulateAssignedCourseData(Student student)
 {
     var allCourses = db.Courses;
     var studentCourses = new HashSet<int>(student.Enrollments.Select(c => c.CourseID));
     var viewModel = new List<StudentEnrollmentCourses>();
     foreach (var course in allCourses)
     {
         viewModel.Add(new StudentEnrollmentCourses
         {
             CourseID = course.CourseID,
             Title = course.Title,
             StudentEnrollment = studentCourses.Contains(course.CourseID)
         });
     }
     ViewBag.Courses = viewModel;
 }