0

i have following entities..

    public class Paper
    {
        public int Id { get; set; }
        public string PaperCode { get; set; }
        ...
    }
    public class MCQPaper : Paper
    {
        public ICollection<MCQQuestion> Questions { get; set; }
    }
    public class MCQQuestion : Question
    {
        public int MCQPaperId { get; set; }
        public MCQPaper MCQPaper { get; set; }

        public int? MCQOptionId { get; set; }
        [ForeignKey("MCQOptionId")]
        public MCQOption TrueAnswer { get; set; }
        public ICollection<MCQOption> MCQOptions { get; set; }
    }
    public class MCQOption
    {
        public int Id { get; set; }
        public string OptionText { get; set; }
    }

and i am trying to fetch MCQPaper based on unique papercode but it always gives me empty collection of Questions

here is my Query inside Repository..

        public MCQPaper GetByPaperCode(string paperCode)
        {
            var ans = AppDbContext.MCQPapers
                .Where(paper => paper.PaperCode.Equals(paperCode))
                //.Include(paper => paper.Questions)
                //    .ThenInclude(que => que.MCQOptions)
                .Include(paper => paper.Questions)
                    .ThenInclude(que => que.MCQPaper)
                //.Include(paper => paper.Questions)
                //    .ThenInclude(que => que.TrueAnswer)
                .FirstOrDefault();
            return ans;
        }

here i have tried various combinations of include() and theninclude() but none of them work for me

and lastly ignore grammer mistakes if any

thankyou in advance

8
  • are you tried to put where after the include ? remove where and put your condition to FirstOrDefaut() .FirstOrDefault(paper => paper.PaperCode.Equals(paperCode)); Commented Oct 16, 2020 at 5:20
  • thank you for suggestion i will try it and let you know Commented Oct 16, 2020 at 5:24
  • @puko still it is not working Commented Oct 16, 2020 at 5:32
  • are you sure that you have some questions inserted in database ? with right foreign key ? Commented Oct 16, 2020 at 5:35
  • Check this article learnentityframeworkcore.com/configuration/… Commented Oct 16, 2020 at 5:41

1 Answer 1

0

after going threw comments and searching on google i have found solution

using two queries this problem can be solved because here i have circular dependency between MCQQuestion and MCQOption

so solution is ....

        public MCQPaper GetByPaperCode(string paperCode)
        {
            using var transaction = AppDbContext.Database.BeginTransaction();
            MCQPaper ans = new MCQPaper();
            try
            {
                ans = AppDbContext.MCQPapers
                .FirstOrDefault(paper => paper.PaperCode.Equals(paperCode));
                var questions = AppDbContext.MCQQuestions
                    .Include(que => que.MCQOptions)
                    .Where(que => que.MCQPaperId == ans.Id);
                ans.Questions = questions.ToList();
                transaction.Commit();
            }
            catch (Exception)
            {
                transaction.Rollback();
            }
            return ans;
        }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.