예제 #1
0
 public void setVariants()
 {
     foreach (var one in this._questionModel.Variants)
     {
         var variant = new Variants();
         variant.variant = one.VariantText;
         variant.questionId = this._questionModel.QuestionId;
         
         question.Variants.Add(variant);
     }
 }
예제 #2
0
        private void SaveTest()
        {
            try
            {
            using (var context = new QuizDBEntities())
            {
                var questionIdx = context.Questions.ToList().Select(t => t.id).Max() + 1;
                var variantIdx = context.Variants.ToList().Select(t => t.id).Max() + 1;
                context.Tests.Add(_currentTest);
                
                foreach (var itemQuestion in _testQuestions)
                {
                    var question = new Questions();
                    question.id = questionIdx++;
                    question.testId = this._currentTest.id;
                    question.question = itemQuestion.QuestionText;
                    //TODO: list variants

                    foreach (var itemVariant in itemQuestion.Variants)
                    {
                        var variant = new Variants();
                        variant.id = variantIdx++;
                        variant.variant = itemVariant.VariantText;
                        variant.imgPath = itemVariant.ImageUri;
                        variant.variant_type = itemVariant.Type;
                        variant.questionId = question.id;
                        if (itemVariant.IsCorrect == true)
                        {
                            var answer = new Answers();
                            answer.questionId = question.id;
                            answer.variantId = variant.id;
                            context.Answers.Add(answer);
                        }
                        question.Variants.Add(variant);
                    }
                    //
                    context.Questions.Add(question);
                }
                context.SaveChanges();
            }

                // Your code...
                // Could also be before try if you know the exception occurs in SaveChanges

                
            }
            catch (DbEntityValidationException e)
            {
                foreach (var eve in e.EntityValidationErrors)
                {
                    var error = String.Format("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                        eve.Entry.Entity.GetType().Name, eve.Entry.State);
                    foreach (var ve in eve.ValidationErrors)
                    {
                        error += string.Format("\n- Property: \"{0}\", Error: \"{1}\"",
                            ve.PropertyName, ve.ErrorMessage);
                    }
                }
                throw;
            }
        }