'Some navigational properties are null in CompareTo when inserting into a SortedSet

I have the following classes as basis for code first:

public class ArenaTeam
{
    [Key]
    public int Id { get; set; }
    public SortedSet<ArenaCharacter> Characters { get; set; }
}

public class ArenaCharacter : IComparable<ArenaCharacter>
{
    [Key]
    public int Id { get; set; }
    public ArenaTeam ArenaTeam { get; set; }
    public int Level { get; set; }
    public BaseCharacter BaseCharacter { get; set; }
    public int CompareTo(ArenaCharacter? other)
    {
        if (other == null)
        {
            return -1000;
        }
        return BaseCharacter.Position - other.BaseCharacter.Position;
    }
}

public class BaseCharacter
{
    [Key]
    public int Id { get; set; }
    public string Name { get; set; }
    public int Position { get; set; }
}

When I try to query the data through for example the query

_db.ArenaTeams.Include(t => t.Characters).ToArray();

the CompareTo method is called but when it is called the navigational property BaseCharacter, which is needed for the comparing, is null while the rest of the properties have their values (including the other navigational property). However it is not a case of BaseCharacter never receiving its value, in the end it has a value and even the ArenaCharacter other that it is comparing to has a non-null BaseCharacter. Is there a way to make sure the BaseCharacter is loaded before CompareTo is called?

I have tried using a .ThenInclude()

_db.ArenaTeams.Include(t => t.Characters).ThenInclude(c => c.BaseCharacter).ToArray();

which seems to make no difference at all (and logically it shouldn't as the value gets set eventually without it anyways). Other than that I have tried looking around on the internet for a solution but have been unable to find one so far.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source