コード例 #1
0
        public void DoImport(List <Work> works)
        {
            using (var dataContext = new SnapDataContext())
            {
                var subjectList           = dataContext.Subjects.ToList();
                var domainList            = dataContext.Domains.ToList();
                var learningObjectiveList = dataContext.LearningObjectives.ToList();

                var count         = 0;
                var progressCount = 0;
                var exerciseIDs   = works.Select(a_item => a_item.ExerciseID)
                                    .Distinct()
                                    .ToList();
                Seeder.ProgressInit(dataContext, Seeder.Exercises, exerciseIDs.Count());
                foreach (var exerciseID in exerciseIDs)
                {
                    var exercise = works.First(a_item => a_item.ExerciseID == exerciseID);

                    var subject           = subjectList.SingleOrDefault(a_item => a_item.Name == exercise.Subject);
                    var domain            = domainList.Single(a_item => a_item.Name == exercise.Domain);
                    var learningObjective =
                        learningObjectiveList.Single(a_item => a_item.Name == exercise.LearningObjective);

                    dataContext.Exercises.Add(new Exercise
                    {
                        ID                = exercise.ExerciseID,
                        Subject           = subject,
                        Domain            = domain,
                        LearningObjective = learningObjective
                    });

                    if (++count == 100)
                    {
                        count          = 0;
                        progressCount += 100;
                        dataContext.SaveChanges();
                        Seeder.ProgressReport(dataContext, Seeder.Exercises, progressCount);
                    }
                }
                dataContext.SaveChanges();
                Seeder.ProgressReport(dataContext, Seeder.Exercises, exerciseIDs.Count());

                var students  = dataContext.Students.ToList();
                var exercises = dataContext.Exercises.ToList();

                count         = 0;
                progressCount = 0;
                Seeder.ProgressInit(dataContext, Seeder.Answers, works.Count());
                foreach (var work in works)
                {
                    var student  = students.Single(a_item => a_item.ID == work.UserID);
                    var exersise = exercises.Single(a_item => a_item.ID == work.ExerciseID);

                    dataContext.Answers.Add(new Answer
                    {
                        ID             = work.SubmittedAnswerID,
                        SubmitDateTime = work.SubmitDateTime,
                        Correct        = work.Correct,
                        Progress       = work.Progress,
                        Difficulty     = work.Difficulty,
                        Exercise       = exersise,
                        Student        = student
                    });

                    if (++count == 100)
                    {
                        count          = 0;
                        progressCount += 100;
                        dataContext.SaveChanges();
                        Seeder.ProgressReport(dataContext, Seeder.Answers, progressCount);
                    }
                }
                dataContext.SaveChanges();
                Seeder.ProgressReport(dataContext, Seeder.Answers, works.Count());
            }
        }
コード例 #2
0
        protected override void Seed(SnapDataContext dataContext)
        {
            base.Seed(dataContext);

            dataContext.Teachers.Add(new Teacher
            {
                FirstName = "Tim",
                LastName  = "Theeuwes",
                ProfilePictureLocation = @"teachers/tim.jpg"
            });

            Seeder.ProgressCreate(dataContext, Seeder.Students);
            Seeder.ProgressCreate(dataContext, Seeder.Domains);
            Seeder.ProgressCreate(dataContext, Seeder.Subjects);
            Seeder.ProgressCreate(dataContext, Seeder.LearningObjectives);
            Seeder.ProgressCreate(dataContext, Seeder.Exercises);
            Seeder.ProgressCreate(dataContext, Seeder.Answers);

            var          assembly     = Assembly.GetExecutingAssembly();
            const string resourceName = "SnapChallenge.Data.Resources.work.csv";

            using (var stream = assembly.GetManifestResourceStream(resourceName))
                using (var streamReader = new StreamReader(stream))
                {
                    var content = streamReader
                                  .ReadToEnd()
                                  .Split(new char[] { '\r', '\n' }, System.StringSplitOptions.RemoveEmptyEntries);

                    var works = content
                                .Skip(1)
                                .Select(Work.ParseCsvIntoWork)
                                .ToList();

                    var studentIDs = works.Select(a_item => a_item.UserID)
                                     .Distinct()
                                     .ToList();
                    Seeder.ProgressInit(dataContext, Seeder.Students, studentIDs.Count());
                    foreach (var studentID in studentIDs)
                    {
                        var profilePicture = studentID % 10;
                        dataContext.Students.Add(new Student
                        {
                            ID = studentID,
                            ProfilePictureLocation = $@"profile-pics/{profilePicture}.jpg"
                        });
                    }
                    Seeder.ProgressReport(dataContext, Seeder.Students, studentIDs.Count());

                    var domains = works.Select(a_item => a_item.Domain)
                                  .Distinct()
                                  .ToList();
                    Seeder.ProgressInit(dataContext, Seeder.Domains, domains.Count());
                    foreach (var domain in domains)
                    {
                        if (domain == null)
                        {
                            continue;
                        }

                        dataContext.Domains.Add(new Domain
                        {
                            Name = domain
                        });
                    }
                    Seeder.ProgressReport(dataContext, Seeder.Domains, domains.Count());

                    var subjects = works.Select(a_item => a_item.Subject)
                                   .Distinct()
                                   .ToList();
                    Seeder.ProgressInit(dataContext, Seeder.Subjects, subjects.Count());
                    foreach (var subject in subjects)
                    {
                        if (subject == null)
                        {
                            continue;
                        }

                        dataContext.Subjects.Add(new Subject
                        {
                            Name = subject
                        });
                    }
                    Seeder.ProgressReport(dataContext, Seeder.Subjects, subjects.Count());

                    var learningObjectives = works.Select(a_item => a_item.LearningObjective)
                                             .Distinct()
                                             .ToList();
                    Seeder.ProgressInit(dataContext, Seeder.LearningObjectives, learningObjectives.Count());
                    foreach (var learningObjective in learningObjectives)
                    {
                        if (learningObjective == null)
                        {
                            continue;
                        }

                        dataContext.LearningObjectives.Add(new LearningObjective
                        {
                            Name = learningObjective
                        });
                    }
                    dataContext.SaveChanges();
                    Seeder.ProgressReport(dataContext, Seeder.LearningObjectives, learningObjectives.Count());

                    var seederThread = new SeederThread();
                    var importThread = new Thread(() => seederThread.DoImport(works))
                    {
                        IsBackground = true
                    };
                    importThread.Start();
                }
        }