public User RegisterUser(User user)
 {
     //_users.Add(user);
     _context.Users.Add(user);
     _context.SaveChanges();
     return(user);
 }
        static void Main(string[] args)
        {
            var context = new EnrollmentContext();

            if (!context.Schedules.Any())
            {
                var schedule = new Schedule()
                {
                    Title = "Schedule 1"
                };

                schedule.Instances.Add(new Instance {
                    Date = DateTime.Now
                });
                schedule.Instances.Add(new Instance {
                    Date = DateTime.Now.AddDays(1)
                });

                context.Schedules.Add(schedule);
                context.SaveChanges();
            }


            var sc = context.Schedules
                     .Include(x => x.Instances)
                     .SingleOrDefault(x => x.Id == 1);

            sc.Instances.Remove(sc.Instances.First());
            context.SaveChanges();


            Console.WriteLine("asd");
        }
        public void Add(EnrollmentRequest request)
        {
            var documents = request.Person.Documents.Select(x => x.Type + x.Number);

            if (_context.Enrollments.Any(x => documents.Contains(x.TypeIdentification + x.Identification)))
            {
                throw new EnrollmentAlreadyExistsException();
            }
            _context.Add(_toInfrastructureMapper.Map(request));
            _context.SaveChanges();
        }
        public void Delete(int id)
        {
            var enrollment = _context.Enrollments.FirstOrDefault(x => x.Id == id);

            if (enrollment == null)
            {
                throw new EnrollmentNotFoundException();
            }
            _context.Remove(enrollment);
            _context.SaveChanges();
        }
        static void InsertCourseWithFK(int id)
        {
            var newCourse = new Course()
            {
                InstructorId = id, Title = "New Chopstick mastery course", StartDate = DateTime.Now, EndDate = DateTime.Now
            };

            using (var newContext = new EnrollmentContext())
            {
                newContext.Courses.Add(newCourse);
                newContext.SaveChanges();
            }
        }
 static void UpdateInstructor(int id)
 {
     using (var context = new EnrollmentContext())
     {
         var instructor = context.Instructors
                          .Include("Courses")
                          .FirstOrDefault(i => i.InstructorId == id);
         instructor.Courses.Add(new Course()
         {
             Title = "Mastering chop sticks 2", StartDate = DateTime.Now, EndDate = DateTime.Now
         });
         context.Instructors.Update(instructor);
         context.SaveChanges();
     }
 }
        static void UpdateUsingEntry()
        {
            var context    = new EnrollmentContext();
            var instructor = context.Instructors
                             .Include(i => i.Courses)
                             .SingleOrDefault(x => x.Name.Contains("Shifu"));

            var course = instructor.Courses[0];

            course.Title = "The first course of master shifu";
            using (var newContext = new EnrollmentContext())
            {
                newContext.Entry(course).State = EntityState.Modified;
                newContext.SaveChanges();
            }
        }
        static void UpdateInstructorDisconnected(int id)
        {
            var context    = new EnrollmentContext();
            var instructor = context.Instructors
                             .Include(i => i.Courses)
                             .FirstOrDefault(i => i.InstructorId == id);

            instructor.Name = "Master Shifu - The true master";

            using (var newContext = new EnrollmentContext())
            {
                newContext.Instructors.Update(instructor);
                newContext.Courses.RemoveRange(instructor.Courses.OrderBy(x => x.CourseId).Last());
                newContext.SaveChanges();
            }
        }
        static void Enroll_Every_Student_In_Clean_Code_Course()
        {
            using (var context = new EnrollmentContext())
            {
                var students  = context.Students.ToList();
                var cleanCode = context.Courses.Find(4);

                students.ForEach(s =>
                {
                    cleanCode.Enrollments.Add(new Enrollment {
                        StudentId = s.StudentId, CourseId = cleanCode.CourseId
                    });
                });

                context.SaveChanges();
            }
        }
        static void InsertRelatedData()
        {
            using (var context = new EnrollmentContext())
            {
                context.Instructors.Add(new Instructor()
                {
                    Name    = "Master Shifu",
                    Courses = new List <Course>
                    {
                        new Course()
                        {
                            Title = "Mastering the troll fu", StartDate = DateTime.Now, EndDate = DateTime.Now
                        }
                    }
                });

                context.SaveChanges();
            }
        }
        static void UpdateInstructorUsingAttach(int id)
        {
            var context    = new EnrollmentContext();
            var instructor = context.Instructors
                             .Include(i => i.Courses)
                             .FirstOrDefault(i => i.InstructorId == id);

            instructor.Name = "Master Shifu - The true master";
            instructor.Courses.Add(new Course()
            {
                Title = "Mastering chop sticks 3", StartDate = DateTime.Now, EndDate = DateTime.Now
            });

            using (var newContext = new EnrollmentContext())
            {
                newContext.Instructors.Attach(instructor);

                newContext.SaveChanges();
            }
        }
        public void Update(int id, EnrollmentRequest request)
        {
            var enrollment = _context.Enrollments.FirstOrDefault(x => x.Id == id);

            if (enrollment == null)
            {
                throw new EnrollmentNotFoundException();
            }

            enrollment.FirstName          = request.Person.Name.First;
            enrollment.MiddleName         = request.Person.Name.Middle;
            enrollment.LastName           = request.Person.Name.Last;
            enrollment.DoB                = request.Person.Info.DateOfBirth;
            enrollment.House              = request.House.ToString();
            enrollment.TypeIdentification = request.Person.Documents.FirstOrDefault()?.Type.ToString();
            enrollment.Identification     = request.Person.Documents.FirstOrDefault()?.Number;
            enrollment.Status             = request.Status.ToString();

            _context.Update(enrollment);
            _context.SaveChanges();
        }
        static void Remove_Jack_Sparrow_From_CleanCode()
        {
            using (var context = new EnrollmentContext())
            {
                var student = context.Students
                              .Include("Enrollments")
                              .FirstOrDefault(s => s.StudentId == 4);

                var course = context.Courses
                             .Include("Enrollments")
                             .FirstOrDefault(c => c.CourseId == 4);

                var enrollment = student.Enrollments.FirstOrDefault(x => x.CourseId == course.CourseId);

                if (enrollment != null)
                {
                    course.Enrollments.Remove(enrollment);
                }
                context.SaveChanges();
            }
        }
        public static void Initialize(EnrollmentContext context)
        {
            context.Database.EnsureCreated();

            // Look for enrollment.
            if (context.Enrollments.Any())
            {
                return;   // DB has been seeded
            }

            var students = new Student[]
            {
                new Student {
                    StudentID = 1, Age = 23, FirstName = "Lebron", LastName = "James", Mail = "*****@*****.**"
                },
                new Student {
                    StudentID = 2, Age = 25, FirstName = "Pablo", LastName = "Aimar", Mail = "*****@*****.**"
                },
                new Student {
                    StudentID = 3, Age = 27, FirstName = "Pablo", LastName = "Escobar", Mail = "*****@*****.**"
                },
                new Student {
                    StudentID = 4, Age = 29, FirstName = "Estrella", LastName = "Torres", Mail = "*****@*****.**"
                },
                new Student {
                    StudentID = 5, Age = 21, FirstName = "Karen", LastName = "Alga", Mail = "*****@*****.**"
                }
            };

            foreach (Student s in students)
            {
                context.Students.Add(s);
            }
            context.SaveChanges();

            var teachers = new Teacher[]
            {
                new Teacher {
                    TeacherID = 1, Name = "Marcelo", LastName = "Bielsa"
                },
                new Teacher {
                    TeacherID = 2, Name = "Erwe", LastName = "Von Esse"
                },
                new Teacher {
                    TeacherID = 3, Name = "Ruben", LastName = "Aguirre"
                }
            };

            foreach (Teacher t in teachers)
            {
                context.Teachers.Add(t);
            }
            context.SaveChanges();

            var courses = new Course[]
            {
                new Course {
                    CourseID = 1050, Title = "Chemistry", IsOpen = true, MaxPaticipants = 10, TeacherID = 1
                },
                new Course {
                    CourseID = 4022, Title = "Microeconomics", IsOpen = false, MaxPaticipants = 10, TeacherID = 2
                },
                new Course {
                    CourseID = 1045, Title = "Calculus", IsOpen = true, MaxPaticipants = 5, TeacherID = 3
                },
                new Course {
                    CourseID = 3141, Title = "Trigonometry", IsOpen = true, MaxPaticipants = 10, TeacherID = 1
                },
                new Course {
                    CourseID = 2021, Title = "Composition", IsOpen = true, MaxPaticipants = 10, TeacherID = 2
                },
                new Course {
                    CourseID = 2042, Title = "Literature", IsOpen = true, MaxPaticipants = 10, TeacherID = 3
                }
            };

            foreach (Course c in courses)
            {
                context.Courses.Add(c);
            }
            context.SaveChanges();

            var enrollments = new Enrollment[]
            {
                new Enrollment {
                    StudentID = 1, CourseID = 1050
                },
                new Enrollment {
                    StudentID = 1, CourseID = 4022
                },
                new Enrollment {
                    StudentID = 1, CourseID = 1045
                },
                new Enrollment {
                    StudentID = 2, CourseID = 1045
                },
                new Enrollment {
                    StudentID = 2, CourseID = 3141
                },
                new Enrollment {
                    StudentID = 2, CourseID = 2021
                },
                new Enrollment {
                    StudentID = 3, CourseID = 1050
                },
                new Enrollment {
                    StudentID = 3, CourseID = 2042
                },
                new Enrollment {
                    StudentID = 4, CourseID = 4022
                },
                new Enrollment {
                    StudentID = 4, CourseID = 1045
                },
                new Enrollment {
                    StudentID = 5, CourseID = 2021
                },
                new Enrollment {
                    StudentID = 5, CourseID = 3141
                },
            };

            foreach (Enrollment e in enrollments)
            {
                context.Enrollments.Add(e);
            }
            context.SaveChanges();
        }
Exemple #15
0
 public bool Save()
 {   // this will return true if saved
     return(_context.SaveChanges() >= 0);
 }
        static void Seed()
        {
            using (var context = new EnrollmentContext())
            {
                if (!context.Instructors.Any())
                {
                    var instructors = new List <Instructor>()
                    {
                        new Instructor {
                            Name = "Uncle Bob"
                        },
                        new Instructor {
                            Name = "Martin Fowler"
                        },
                        new Instructor {
                            Name = "Kent Beck"
                        }
                    };

                    context.AddRange(instructors);
                }

                if (!context.Students.Any())
                {
                    var students = new List <Student>()
                    {
                        new Student()
                        {
                            Name = "Taz Uddin"
                        },
                        new Student()
                        {
                            Name = "Batman the Bat Man"
                        },
                        new Student()
                        {
                            Name = "Popey The Sailor Man"
                        },
                        new Student()
                        {
                            Name = "Jack Sparrow"
                        },
                    };
                    context.AddRange(students);
                }

                if (!context.Courses.Any())
                {
                    var courses = new List <Course>()
                    {
                        new Course()
                        {
                            Title = "The future of programming", InstructorId = 1, StartDate = DateTime.UtcNow, EndDate = DateTime.UtcNow.AddDays(30)
                        },
                        new Course()
                        {
                            Title = "Making architecture matter", InstructorId = 2, StartDate = DateTime.UtcNow, EndDate = DateTime.UtcNow.AddDays(30)
                        },
                        new Course()
                        {
                            Title = "Extreme Programming in practice", InstructorId = 3, StartDate = DateTime.UtcNow, EndDate = DateTime.UtcNow.AddDays(30)
                        },
                        new Course()
                        {
                            Title = "Clean Code", InstructorId = 1, StartDate = DateTime.UtcNow, EndDate = DateTime.UtcNow.AddDays(30)
                        },
                    };
                    context.AddRange(courses);
                }
                context.SaveChanges();
            }
        }