Пример #1
0
        public int SeedResult()
        {
            // ** DEV NOTES ** Will encounter threading error if the ToList() method is not called in here
            // -- on the foreach loops. This loads data into memory ...
            // OR the SaveChanges() could be performed at the end of the cycle.

            // initialize objects
            var sessions = _sessionUser.GetAll().ToList();   // 1000 sessions for 10 surveys
            //var surveys = _surveyMaster.GetAll();   // 10
            //var questions = _question.GetAll();     // 25 questions/survey
            //var answerOption = _answerOption.Get(0);
            Random rnd = new Random();

            //for (var x = 1; x <= sessions.Count(); x++)
            foreach (var s in sessions)
            {
                // get the session and survey
                var session = _sessionUser.Get(s.Id);
                var survey  = _surveyMaster.Get(s.SurveyMaster.Id);

                // get all questions for (each) survey
                var questions = _surveyQuestion.GetAllBySurveyId(s.SurveyMaster.Id).ToList();
                foreach (var q in questions)
                {
                    // create a single question object
                    var question = _question.Get(q.Question.Id);

                    // get a random answer 1-5
                    int option       = rnd.Next(1, 5);
                    var answerOption = _answerOption.Get(option);

                    var newResult = new Result
                    {
                        Session      = session,
                        SurveyMaster = survey,
                        Question     = question,
                        AnswerOption = answerOption
                    };

                    _result.Add(newResult);
                }
            }
            return(1);
        }