I am using EF Core 3.1.3 and I am have these two queries:
var test = await _unitOfWork.Context.PersonEmail
.OrderBy(e => e.EmailId)
.Take(10)
.Include(e => e.EmailRecord)
.ToListAsync();
var test2 = await _unitOfWork.Context.PersonEmail
.OrderBy(e => e.EmailId)
.Take(10)
.ToListAsync();
I was expecting both queries to return 10 results, but for 6 of the items from the test
query to have null
for the EmailRecord
property (since the EmailRecord table does not contain an entry for those particular items). However what actually happens is that the test
query returns only 4 results, whilst the test2
query returns the expected 10.
The entities are declared like this:
[Table("PersonEmail")]
public class PersonEmail
{
public Guid PersonId { get; set; }
public Guid EmailId { get; set; }
[ForeignKey("PersonId, EmailId")]
public EmailRecord EmailRecord { get; set; }
}
[Table("EmailRecord")]
public class EmailRecord
{
public Guid PersonId { get; set; }
public Guid EmailId { get; set; }
public DateTimeOffset LastUpdated { get; set; }
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<PersonEmail>().HasKey(x => new { x.PersonId, x.EmailId });
modelBuilder.Entity<EmailRecord>().HasKey(x => new { x.PersonId, x.EmailId });
}