0

have no reason why that code acts like showed, any ideas?

using DriverLicensePracticerApi.Entities;
using System.Linq;

namespace DriverLicensePracticerApi.Services
{
    public class CategoryService
    {
        private readonly ApplicationDbContext _dbContext;

        public CategoryService(ApplicationDbContext dbContext)
        {
            _dbContext = dbContext;
        }

        public void setupCategories(string categoriesToSplit, int questionId)
        {
            
            var question = _dbContext.Questions.FirstOrDefault(x => x.Id == questionId);
            var splittedCategories = categoriesToSplit.Split(',').ToList();
            foreach (var category in splittedCategories)
            {
                
                switch (category)
                {
                    case "A":
                        var wtf = _dbContext.Categories.FirstOrDefault(x => x.Id == 1);
                        question.Categories.Add(_dbContext.Categories.FirstOrDefault(x => x.Id == 1));
                        break;
                    case "B":
                        question.Categories.Add(_dbContext.Categories.FirstOrDefault(x => x.Id == 2));
                        break;
                    case "C":
                        question.Categories.Add(_dbContext.Categories.FirstOrDefault(x => x.Id == 3));
                        break;
                    case "D":
                        question.Categories.Add(_dbContext.Categories.FirstOrDefault(x => x.Id == 4));
                        break;
                    case "T":
                        question.Categories.Add(_dbContext.Categories.FirstOrDefault(x => x.Id == 5));
                        break;
                    case "AM":
                        question.Categories.Add(_dbContext.Categories.FirstOrDefault(x => x.Id == 6));
                        break;
                    case "A1":
                        question.Categories.Add(_dbContext.Categories.FirstOrDefault(x => x.Id == 7));
                        break;
                    case "A2":
                        question.Categories.Add(_dbContext.Categories.FirstOrDefault(x => x.Id == 8));
                        break;
                    case "B1":
                        question.Categories.Add(_dbContext.Categories.FirstOrDefault(x => x.Id == 9));
                        break;
                    case "C1":
                        question.Categories.Add(_dbContext.Categories.FirstOrDefault(x => x.Id == 10));
                        break;
                    case "D1":
                        question.Categories.Add(_dbContext.Categories.FirstOrDefault(x => x.Id == 11));
                        break;
                    case "PT":
                        question.Categories.Add(_dbContext.Categories.FirstOrDefault(x => x.Id == 12));
                        break;
                }
            }
        }
    }
}

enter image description here

enter image description here

Guru Stron
  • 102,774
  • 10
  • 95
  • 132
bialob
  • 21
  • 3
  • Not really, as showed on screenshots, its not null return query, but in function acts like it is – bialob Jan 02 '22 at 23:59
  • Ok, `wtf` is not null, what about `question` ? – Guru Stron Jan 02 '22 at 23:59
  • I would guess either `question.Categories` is null (Empty list won't cause it), or the First dbContext.Category returned by dbContext.Categories for .FirstOrDefault is null. You can break it into 2 lines and see what return as null or exception. – Orel Eraki Jan 03 '22 at 00:01
  • Also no need to query database in loop - map `splittedCategories` to list of ids and use query with `listOfIds.Contains` logic. – Guru Stron Jan 03 '22 at 00:02
  • question is not null too, the question.Categories is prop from model, should i initialize it somehow before adding objects to it? – bialob Jan 03 '22 at 00:04
  • Looks like a ‘question.Categories’ is null. Add ‘.Include(x=>x.Categories)’ to the line where you query db for ‘question’: ‘ var question = _dbContext.Questions.Include(x=>x.Categories).FirstOrDefault(x => x.Id == questionId);’ – Lukasz Mk Jan 03 '22 at 00:11
  • Yes, after checking it is. So there is question should i initialize before referring to it? – bialob Jan 03 '22 at 00:13
  • Thought it creates with model creation – bialob Jan 03 '22 at 00:13
  • Sorry it’s hard to replay on the phone… Add ‘.Include(x=>x.Categories)’ to the line where you query db for ‘question’: ‘var question = _dbContext.Questions.Include(x=>x.Categories).FirstOrDefault(x => x.Id == questionId);’ – Lukasz Mk Jan 03 '22 at 00:16
  • Include() in EF is translated to ‘join’ in the SQL. EntityFramework doesn’t include sub-entities by default - you need be explicit about that… See more in EF docs. – Lukasz Mk Jan 03 '22 at 00:18
  • ` var toAdd = _dbContext.Categories.FirstOrDefault(x => x.Id == 1); ` This shows as object of Category, added Include Statement and still question.Categories is null – bialob Jan 03 '22 at 00:25
  • In model i declare that list like that ' public List Categories { get; set; } ' maybe it is wrong way? – bialob Jan 03 '22 at 00:26
  • But that’s different. Categories are in the db, but aren’t included/joint to the ‘question’ by default… you need explicitly include categories to question. This is EF feature. More information can be found in the EF docs under include and navigation properties sections. – Lukasz Mk Jan 03 '22 at 00:28
  • Did you try this ‘var question = _dbContext.Questions.Include(x=>x.Categories).FirstOrDefault(x => x.Id == questionId);’ ? – Lukasz Mk Jan 03 '22 at 00:30
  • var question = _dbContext.Questions.Include(x => x.Categories).FirstOrDefault(x => x.Id == questionId); As you mentioned before i did it, and variable var questionCategories = question.Categories; still returns null – bialob Jan 03 '22 at 00:34
  • Hmm… I don’t remember how it works if collection under navigation property is empty… I would expect that collection to be initialised anyway. Unfortunately I have no access to my PC right now so I cannot test that. – Lukasz Mk Jan 03 '22 at 00:41
  • Perhaps relation between question and categories is not correct either in the db or db context. – Lukasz Mk Jan 03 '22 at 00:45
  • Try to search SO or Google why navigation property is null after include. – Lukasz Mk Jan 03 '22 at 00:47
  • And one more - check if ‘question.Categories’ is declared as virtual property in your model? (assuming you are doing code first). That’s required for Include to work. – Lukasz Mk Jan 03 '22 at 00:57

0 Answers0