Exemplo n.º 1
0
        /// <summary>
        /// Populate database
        /// </summary>
        /// <returns>Task bool: True if success, false otherwise</returns>
        public async Task <bool> LoadAsync(int amount)
        {
            try
            {
                var subjects = mitComputerScience.Select(s => new Predmet {
                    Tema = s
                });
                var rand           = new Random(Environment.TickCount);
                var studentsResult = from name in names
                                     from surname in surnames
                                     select new Student
                {
                    Ime     = name,
                    Prezime = surname,
                    Grad    = cities[rand.Next(cities.Length)]
                };
                var students = studentsResult.OrderBy(a => Guid.NewGuid()).Take(amount).Select(s => new Student
                {
                    Ime     = s.Ime,
                    Prezime = s.Prezime,
                    Grad    = s.Grad,
                    Adresa  = "Adresa od " + s.Grad
                }).ToList();

                db.Predmets.AddRange(subjects);
                db.Students.AddRange(students);

                foreach (var student in students)
                {
                    foreach (var subject in subjects)
                    {
                        var scoreLevel = levels[rand.Next(levels.Length)];
                        var grade      = ((scoreLevel == Level.Bad) ?
                                          rand.Next(5, 7) :
                                          (
                                              (scoreLevel == Level.Good) ?
                                              rand.Next(7, 9) : rand.Next(9, 11)
                                          )
                                          );

                        var exam = new Ispit {
                            Predmet = subject, Ocena = grade
                        };
                        db.Ispits.Add(exam);
                        var studentIspit = new StudentIspit {
                            Student = student, Ispit = exam
                        };
                        db.StudentIspits.Add(studentIspit);
                    }
                }

                int returnValue = await db.SaveChangesAsync();

                return(returnValue > 0);
            }
            catch (Exception)
            {
                return(false);
            }
        }