mysql.netentity-frameworkasp.net-coredomain-model

The Problems List is not Populated successfully in the Get Level API


Using .NET Core and EF Im trying to make a list of level for all level it has some problems when I create the problem and the level and then add the problem to the level Using an Api that I create I have this as response enter image description here

which exactly like i want

but when I try to get the level itself i dont have the problems list populated

enter image description here

in the Database I have the problem is refernced with the levelID but cant find the problem list in the Level TABLE in DB However I added it to the Domain Model in the Code

Please find the pictures below

enter image description here

enter image description here

enter image description here


Solution

  • Eager load Problems when reading your Level using .Include(x => x.Problems).

    Also initialize your List of problems either on declaration

    public List<Problem> Problems { get; private set;} = new(); 
    

    ... or in the constructor, not ad-hoc when accessed. With collections you should ensure they are initialized once, and protect their setters to avoid misuse which will muck up EF if abused.