Entity Framework query throws System.NullReferenceException when executing

I’m trying to execute this method:

public async Task<List<JobDetail>?> GetJobDetailsByJobIdAsync(int JobId)
{
    using ApplicationDbContext context = await _factory.CreateDbContextAsync();
    var jobDetails = await context.JobDetails
            .Where(q => q.JobId == JobId)
            .ToListAsync();
    return jobDetails;
}

Every time that var jobDetails line is exectued I get a:

System.NullReferenceException: 'Object reference not set to an instance of an object.'

JobId, always has a valid value.

This is the JobDetail model:

public partial class JobDetail : IAuditable
{
    public int Id { get; set; }
    public byte[] RowVersion { get; set; } = null!;
    public int LineNumber { get; set; }
    public string? Standard { get; set; }
    public int Qty { get; set; } = 1;
    public string? Description { get; set; }
    public string? DrawingNumber { get; set; }
    public string? Size { get; set;}
    public string? BatchReference { get; set; }
    public decimal UnitPrice { get; set; }
    public bool Certificate { get; set; }
    public string? Notes { get; set; }
    public int Released { get; set; }
    public int Balance { get; set; }
    public int JobId { get; set; }
    public int? MaterialId { get; set; }
    public int? ProcessId { get; set; }
    public decimal LineTotal { get; set; }
    public decimal LineTotalRemaining { get; set; }
    public string? CreatedBy { get; set; }
    public DateTimeOffset? Created { get; set; }
    public string? ModifiedBy { get; set; }
    public DateTimeOffset? Modified { get; set; }
    public virtual ICollection<DespatchDetail> DespatchDetails { get; set; } = new List<DespatchDetail>();

    public virtual Job Job { get; set; } = null!;
    public virtual Material? Material { get; set; }
    public virtual Process? Process { get; set; }
    public virtual ICollection<SalesDetail> SalesDetails { get; set; } = new List<SalesDetail>();
}

And this is the table definition in the DbContext:

public virtual DbSet<JobDetail> JobDetails { get; set; }

modelBuilder.Entity<JobDetail>(entity =>
{
    entity.Property(e => e.BatchReference).HasMaxLength(255);
    entity.Property(e => e.CreatedBy).HasMaxLength(255);
    entity.Property(e => e.Description)
        .HasMaxLength(255)
        .HasDefaultValue("");
    entity.Property(e => e.Size)
        .HasMaxLength(255)
        .HasDefaultValue("");
    entity.Property(e => e.DrawingNumber).HasMaxLength(255);
    entity.Property(e => e.JobId).HasColumnName("JobDetail_Job");
    entity.Property(e => e.MaterialId).HasColumnName("JobDetail_Material");
    entity.Property(e => e.ProcessId).HasColumnName("JobDetail_Process");
    entity.Property(e => e.LineTotal).HasColumnType("decimal(18, 2)");
    entity.Property(e => e.LineTotalRemaining).HasColumnType("decimal(18, 2)");
    entity.Property(e => e.ModifiedBy).HasMaxLength(255);
    entity.Property(e => e.RowVersion)
        .IsRowVersion()
        .IsConcurrencyToken();
    entity.Property(e => e.Standard).HasMaxLength(255);
    entity.Property(e => e.UnitPrice).HasColumnType("decimal(18, 2)");

    entity.HasOne(d => d.Job).WithMany(p => p.JobDetails)
        .HasForeignKey(d => d.JobId)
        .HasConstraintName("JobDetail_Job");

    entity.HasOne(d => d.Material).WithMany(p => p.JobDetails)
        .HasForeignKey(d => d.MaterialId)
        .HasConstraintName("JobDetail_Material");

    entity.HasOne(d => d.Process).WithMany(p => p.JobDetails)
        .HasForeignKey(d => d.ProcessId)
        .HasConstraintName("JobDetail_Process");
});

Everything seems to look fine to me. It should just return the collection of JobDetail for a Job where q.JobId == JobId.

However, as mentioned above every time that line is hit, it throws the exception.

A little context however, this is an old Lightswitch app that is being rewritten in Blazor. I took the models and DbContext configuration from Lightswitch. This has worked perfectly for the other 25+ tables so don’t see any real reason why that should cause an issue but I just wanted to point that out.

There are 10,000s records in that table. Please tell me I’m missing something glaringly obvious because it’s driving me insane.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật