实体框架查询在执行时抛出 System.NullReferenceException

问题描述 投票:0回答:1

我正在尝试执行这个方法:

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;
}

每次执行

var jobDetails
行时,我都会收到此错误:

System.NullReferenceException:未将对象引用设置为对象的实例。

JobId
始终具有有效值。

这是

JobDetail
模型类:

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>();
}

这是

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");
});

我觉得一切看起来都很好。它应该只返回

JobDetail
的集合,其中
Job
但是,如上所述,每次命中该行时,都会引发异常。

但是,有一点上下文,这是一个旧的 Lightswitch 应用程序,正在 Blazor 中重写。我从 Lightswitch 获取了模型和

q.JobId == JobId

配置。这对于其他 25 个以上的桌子来说非常有效,所以看不出任何真正的原因会导致问题,但我只是想指出这一点。

该表中有 10,000 条记录。请告诉我我错过了一些明显的东西,因为它让我发疯。

该行执行之前的堆栈跟踪:

enter image description here 之后:

enter image description here

asp.net entity-framework .net-core entity-framework-core blazor
1个回答
0
投票

复制并粘贴我在对话框组件中创建的代码。
  1. 删除了该组件。
  2. 重新创建了组件。
  3. 复制并粘贴代码。
  4. 第一次工作!

在尝试主页上的代码以查看它是否与对话框有关并且它第一次工作后,我来到了这个过程。我阅读了 Radzen 文档/帮助,但没有看到任何表明我正在做的事情会出现问题的内容。

这导致我决定重新创建该组件。你知道吗,它确实有效。浪费了一整天,但希望这对将来的人有帮助。

出于好奇,我会进一步调查它,如果我能提出一些实质性的东西来解释可能导致问题的原因,我会将其发布在这里。

© www.soinside.com 2019 - 2024. All rights reserved.