Пример #1
0
        public void SaveChanges(Questions ques)
        {
            using (IPTSE_EXAMEntities db = new IPTSE_EXAMEntities())
            {
                using (var dbcxtransaction = db.Database.BeginTransaction())
                {
                    try
                    {
                        tbl_Question tbl_Ques = new tbl_Question();
                        tbl_Ques.Id                   = ques.Id;
                        tbl_Ques.QuestionText         = ques.QuestionText;
                        tbl_Ques.QuestionCategoryId   = (int)ques.QuestionCategoryId;
                        tbl_Ques.QuestionDifficultyId = ques.QuestionDifficultyId;
                        tbl_Ques.IsActive             = ques.IsActive;
                        tbl_Ques.Points               = 1;
                        tbl_Ques.CreatedBy            = "Admin";
                        tbl_Ques.CreatedDate          = DateTime.Now;
                        ques.questionsChoice.ForEach(m => tbl_Ques.tbl_Question_Choice.Add(new tbl_Question_Choice
                        {
                            Id          = m.Id,
                            Question_Id = m.Question_Id,
                            ChoiceText  = m.ChoiceText,
                            IsAnswer    = m.IsAnswer,
                            IsActive    = m.IsActive,
                            CreatedBy   = "Admin",
                            CreatedDate = tbl_Ques.CreatedDate
                        }));

                        db.Entry(tbl_Ques).State = EntityState.Modified;

                        tbl_Test_Question_Map map = db.tbl_Test_Question_Map.FirstOrDefault(m => m.QuestionId == ques.Id);

                        //tbl_Ques.tbl_Test_Question_Map.Add(new tbl_Test_Question_Map
                        //{
                        //    ID = map.ID,
                        //    IsActive = true,
                        //    QuestionId = ques.Id,
                        //    TestId = ques.TestId,
                        //    CreatedBy = "Admin",
                        //    CreatedDate = tbl_Ques.CreatedDate
                        //});

                        foreach (var item in tbl_Ques.tbl_Test_Question_Map)
                        {
                            item.TestId          = ques.TestId;
                            db.Entry(item).State = EntityState.Modified;
                        }

                        foreach (var item in tbl_Ques.tbl_Question_Choice)
                        {
                            db.Entry(item).State = EntityState.Modified;
                        }

                        db.SaveChanges();
                        dbcxtransaction.Commit();
                    }
                    catch (DbEntityValidationException ex)
                    {
                        dbcxtransaction.Rollback();
                        throw ex;
                    }
                    catch (Exception ex)
                    {
                        dbcxtransaction.Rollback();
                        throw ex;
                    }
                }
            }
        }
Пример #2
0
        public void AddQuestion(Questions in_question)
        {
            using (IPTSE_EXAMEntities db = new IPTSE_EXAMEntities())
            {
                using (var dbcxtransaction = db.Database.BeginTransaction())
                {
                    try
                    {
                        tbl_Question          question          = new tbl_Question();
                        tbl_Question_Category question_Category = new tbl_Question_Category();
                        tbl_Question_Choice   question_Choice   = new tbl_Question_Choice();
                        //tbl_Test test = new tbl_Test();
                        tbl_Test_Question_Map test_question_Map = new tbl_Test_Question_Map();

                        question.QuestionText         = in_question.QuestionText;
                        question.IsActive             = in_question.IsActive;
                        question.Points               = in_question.Points;
                        question.QuestionCategoryId   = (int)in_question.QuestionCategoryId;
                        question.QuestionDifficultyId = in_question.QuestionDifficultyId;
                        question.CreatedBy            = "Admin";
                        question.CreatedDate          = DateTime.Now;
                        question.Points               = 1;
                        db.tbl_Question.Add(question);
                        db.SaveChanges();
                        //question.Id

                        db.tbl_Test_Question_Map.Add(new tbl_Test_Question_Map
                        {
                            TestId      = in_question.TestId,
                            QuestionId  = question.Id,
                            IsActive    = in_question.IsActive,
                            CreatedBy   = "Admin",
                            CreatedDate = DateTime.Now
                        });

                        foreach (QuestionsChoice item in in_question.questionsChoice)
                        {
                            db.tbl_Question_Choice.Add(new tbl_Question_Choice
                            {
                                ChoiceText  = item.ChoiceText,
                                IsActive    = item.IsActive,
                                IsAnswer    = item.IsAnswer,
                                Question_Id = question.Id,
                                CreatedBy   = "Admin",
                                CreatedDate = DateTime.Now
                                              //,tbl_Question = question
                            });
                        }
                        db.SaveChanges();
                        dbcxtransaction.Commit();
                    }
                    catch (DbEntityValidationException ex)
                    {
                        dbcxtransaction.Rollback();
                        throw ex;
                    }
                    catch (Exception ex)
                    {
                        dbcxtransaction.Rollback();
                        throw ex;
                    }
                }
            }
        }