locked
Database scope for many to many ASP.NET RRS feed

  • Question

  • User-1225046321 posted

    i have problem with many-to-many relationship. ive implemented GenericRepository and UnitOfWork pattern.

    i have 2 entities, Test and Question

    the problem is that when i call these 2 entities from database and want to link them in link table ef is duplicating then in their respective tables. I believe this could be solved with using(var context = new MyDbContext()) statement and linking them inside that using statement but unfortunately UnitOfWork dont allow that, so ive tried using (var scope = new TransactionScope()) with no luck.

    for example: i have 5 questions in database and 3 answers for each question and after user is done with test i figure out user score and insert new test row with some data. link table is here to show what questions did user had for that test. when i save test entity every single question and answer is duplicated to database.

    here is the code for reference, thanks

        public int SaveTestResult(TestEntity testEntity)
        {
            using (var scope = new TransactionScope() )
            {
    
                testEntity.Questions = testEntity.Questions.OrderBy(q => q.Id).ToList();
                var idlist = testEntity.Questions.Select(x => x.Id).ToList(); // get ids from list of questions from testentity
    
                //note:i believe this line is causing that behaviour 
                var questionss = _unitOfWork.QuestionRepository.GetMany(q => idlist.Contains(q.Id)).OrderBy(q => q.Id).ToList();//get questions from ids and sort them by id  
                var questions = AutoMapper.Mapper.Map<List<Question>, List<QuestionEntity>>(questionss); //map from Question from db to QuestionEntity
                testEntity.Questions = testEntity.Questions.OrderBy(q => q.Id).ToList();//sort testEtities questions id to match questions above
                var number= 0;
                for (int i = 0; i < testEntity.Questions.Count; i++)
                {
                    for (int j = 0; j < testEntity.Questions[i].Answers.Count; j++)
                    {
                        if ((testEntity.Questions[i].Answers[j].IsTrueUserChoice == questions[i].Answers[j].IsTrue) && (questions[i].Answers[j].IsTrue == true))
                            number++;
                    }
                }
    
                testEntity.TestScore = number;
                testEntity.Questions = questions;//ading questions from db so model validation would pass. quesiton from testEntiy are not valid, some properties are missing
                var testDb = new Test();
                testDb.TestTime = (testDb.TestDateEnd - testEntity.TestDateStart).Value;
                testDb.TestDateStart = testEntity.TestDateStart.Value;
                testDb.TestScore = testEntity.TestScore;
                testDb.UserId = testEntity.UserId;
                testDb.Questions = AutoMapper.Mapper.Map<List<QuestionEntity>, List<Question>>(testEntity.Questions);
                _unitOfWork.TestRepository.Insert(testDb);
                _unitOfWork.Save();
                scope.Complete();
                return number;
            }
        }
    Thursday, June 21, 2018 9:49 PM

All replies

  • User-1811426859 posted

    please post the models and how the context like to help reproduce .

    Friday, June 22, 2018 9:03 AM