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 executed, I get this error:
System.NullReferenceException: Object reference not set to an instance of an object.
JobId always has a valid value.
This is the JobDetail model class:
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.

