I have an odd issue when retrieving data from the database. This is a project that is being upgraded from .NET Core 1.0 towards .NET Core 2.1. Everything was working beforehand, but the upgrade has some strange side effects when loading related data. The exact versions used are 2.1 for .NETCore.app; 2.1.4 for AspNetCore, 2.1.4 for EntityFrameworkCore(.Sqlite).
The problem can be expressed using the following models. Upon insertion, first a Match
is added without the related Result
entities. Then, each Result
is created and finally the Match
is updated again with these instances. This is the reason for the nullable ID fiels in Match
.
public class Result
{
public int Id { get; set; }
public int MatchId { get; set; }
public Match Match { get; set; }
}
public class Match
{
public int Id { get; set; }
public int? HomeId { get; set; }
public int? AwayId { get; set; }
public Result Home { get; set; }
public Result Away { get; set; }
}
What happens is when I retrieve data (for example, context.Results.Include(r => r.Match)
) and inspect the results, every other result misses the related data. This only seems to happen with these tables, so my guess is something goes astray due to the bidirectionality of the models.
The inspector shows a result like below. It should be noted that sets of two consecutive results always points towards the same Match
.
results
- [0]
Id = 1000
Match = Null
- [1]
Id = 1001
Match = <Match object>
- [2]
Id = 1002
Match = Null
- [3]
Id = 1003
Match = <Match object>
And so on. I have troubles inserting data into these tables as well (similar issues pop up), but let's keep that out of the scope for this question at this point.