Example #1
0
        static void Main()
        {
            Database.SetInitializer(new MigrateDatabaseToLatestVersion<StudentsSystemContext, Configuration>());
            
            using (StudentsSystemContext dbContext = new StudentsSystemContext())
            {
                var firstHomework = new Homework()
                {
                    Content = "Simple database1",
                    TimeSent = DateTime.Now,
                };

                var secondHomework = new Homework()
                {
                    Content = "Simple database2",
                    TimeSent = DateTime.Now,
                };

                var student = new Student()
                {
                    FirstName = "Nikolay",
                    LastName = "Kostadinov",
                    Number = "965552",
                };

                var course = new Course()
                {
                    Name = "DataBase",
                    Description = "Entity Framework Code First",
                    Materials = "Lections, homework"
                };

                student.Homeworks.Add(firstHomework);
                student.Homeworks.Add(secondHomework);
                student.Courses.Add(course);
                course.Homeworks.Add(secondHomework);
                course.Students.Add(student);

                dbContext.Students.Add(student);
                dbContext.Courses.Add(course);
                dbContext.Homeworks.Add(firstHomework);
                dbContext.Homeworks.Add(secondHomework);

                dbContext.SaveChanges();
            }
        }
        public IHttpActionResult Create(CourseModel item)
        {
            if (!this.ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            var newItem = new Course()
            {
                Name = item.Name
            };

            this.data.Courses.Add(newItem);
            this.data.Courses.SaveChanges();

            item.ID = newItem.ID;

            return Ok(item);
        }
        public void Seed(int count)
        {
            Console.Write("Importing courses:");

            var db = new StudentsSystemDbContext();
            var allStudents = db
                .Students
                .OrderBy(s => Guid.NewGuid())
                .ToList();
            var allHomeworks = db
                .Homeworks
                .OrderBy(h => Guid.NewGuid())
                .ToList();

            for (int i = 0; i < count; i++)
            {
                var currentStudentInCourse = RandomGenerator.GetRandomNumber(10, 30);
                var currentStartIndex = RandomGenerator.GetRandomNumber(0,
                    allStudents.Count - currentStudentInCourse - 1);
                var studentsList = new HashSet<Student>();
                for (int j = 0; j < currentStudentInCourse; j++)
                {
                    studentsList.Add(allStudents[currentStartIndex]);
                    currentStartIndex++;
                }

                var currentHomeworsInCourse = RandomGenerator.GetRandomNumber(5, 15);
                currentStartIndex = RandomGenerator.GetRandomNumber(0,
                    allHomeworks.Count - currentHomeworsInCourse - 1);
                var homeworksList = new HashSet<Homework>();
                for (int j = 0; j < currentHomeworsInCourse; j++)
                {
                    homeworksList.Add(allHomeworks[currentStartIndex]);
                    currentStartIndex++;
                }

                var course = new Course
                {
                    Name = RandomGenerator.GetRandomString(10, 100),
                    Description = RandomGenerator.GetRandomString(50, 500),
                    Students = studentsList,
                    Homeworks = homeworksList
                };

                db.Courses.Add(course);

                if (i % 10 == 0)
                {
                    Console.Write(".");
                }

                if (i % 100 == 0)
                {
                    db.SaveChanges();
                    db.Dispose();
                    db = new StudentsSystemDbContext();
                }
            }

            db.SaveChanges();
            Console.WriteLine();
        }