예제 #1
0
 private static void PrepareDb(StudentDbContext ctx)
 {
     ctx.Database.EnsureDeleted();
     ctx.Database.EnsureCreated();
 }
예제 #2
0
        private static void SeedData(StudentDbContext context)
        {
            //students
            Console.WriteLine("Adding students..");
            const int totalStudents = 25;
            const int totalCourses  = 10;
            var       currentDate   = DateTime.Now;

            for (int i = 0; i < totalStudents; i++)
            {
                context.Students.Add(new Student
                {
                    Name             = $"Student {i}",
                    RegistrationDate = currentDate.AddDays(i),
                    Birthday         = currentDate.AddYears(-20).AddDays(i),
                    PhoneNumber      = $"Random Phone {i}"
                });
            }

            context.SaveChanges();

            //courses
            var addedCourses = new List <Course>();

            for (int i = 0; i < totalCourses; i++)
            {
                var course = new Course
                {
                    Name        = $"Course {i}",
                    Description = $" Course Details {i}",
                    Price       = 100 * i,
                    StartDate   = currentDate.AddDays(i),
                    EndDate     = currentDate.AddDays(20 + i)
                };
                addedCourses.Add(course);
                context.Courses.Add(course);
            }

            context.SaveChanges();
            // Students in courses
            var studentsIds = context
                              .Students
                              .Select(s => s.Id)
                              .ToList();

            for (int i = 0; i < totalCourses; i++)
            {
                var currentCourse    = addedCourses[i];
                var studentsInCourse = random.Next(2, totalStudents / 2);
                for (int j = 0; j < studentsInCourse; j++)
                {
                    var studentId = studentsIds[random.Next(0, studentsIds.Count)];

                    if (!currentCourse.Students.Any(s => s.StudentId == studentId))
                    {
                        currentCourse.Students.Add(new StudentsCourses
                        {
                            StudentId = studentId
                        });
                    }
                    else
                    {
                        j--;
                    }
                }
                var resourcesInCourse = random.Next(2, 20);
                var types             = new[] { 0, 1, 2, 999 };
                for (int j = 0; j < resourcesInCourse; j++)
                {
                    currentCourse.Resources.Add(new Resource
                    {
                        Name         = $"Resource {i} {j}",
                        Url          = $"URL{i} {j}",
                        ResourceType = (TypeOfResource)types[random.Next(0, types.Length)],
                    });
                }
            }
            context.SaveChanges();
            // Homeworks
            for (int i = 0; i < totalCourses; i++)
            {
                var currentCourse       = addedCourses[i];
                var studentsInCourseIds = currentCourse
                                          .Students
                                          .Select(s => s.StudentId)
                                          .ToList();
                for (int j = 0; j < studentsInCourseIds.Count; j++)
                {
                    var totalHomework = random.Next(2, 5);
                    for (int k = 0; k < totalHomework; k++)
                    {
                        context.Homeworks.Add(new Homework
                        {
                            Content        = $"Content Homework {i}",
                            SubmissionDate = currentDate.AddDays(-i),
                            ContentType    = ContentType.Zip,
                            StudentId      = studentsInCourseIds[j],
                            CourseId       = currentCourse.Id
                        });
                    }
                }

                context.SaveChanges();
            }
        }