You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ASP.NET Core MVC单表数据获取失败,抛出SqlNullValueException

问题分析与解决方案

问题现象

此前功能运行正常,目前仅VerDummyWellGeneralInformation表无法获取数据,其余数据表均正常。系统抛出异常:

SqlNullValueException: Data is Null. This method or property cannot be called on Null values.

断点调试可见数据库中存在对应数据,但无法正常加载。

相关代码

控制器代码

public IActionResult IndexWGI()
{
    IEnumerable<VerDummyWellGeneralInformation> wgi = _db.VerDummyWellGeneralInformations;
    return View(wgi);
}

其余Index方法逻辑与此一致,但仅该方法报错。

模型代码

public partial class VerDummyWellGeneralInformation
{
    [Key] public int No { get; set; }
    public string? Well { get; set; }
    public string? OriginalOwnerCompany { get; set; }
    public string? OwnerCompany { get; set; }
    public string? OriginalOperatorCompany { get; set; }
    public string? OperatorCompany { get; set; }
    public string? WellName { get; set; }
    public string? WellShortName { get; set; }
    public string? LocationName { get; set; }
    public string? UniqueWellId { get; set; }
    public string? BoreholeName { get; set; }
    public string? BoreholeShortName { get; set; }
    public string? UniqueBoreholeId { get; set; }
    public string? AliasName { get; set; }
    public string? Field { get; set; }
    public string? Structure { get; set; }
    public string? Basin { get; set; }
    public string? District { get; set; }
    public string? County { get; set; }
    public string? Province { get; set; }
    public string? Country { get; set; }
    public string? IdPepmaps { get; set; }
    public string? Latitude { get; set; }
    public string? Longitude { get; set; }
    public string? BottomLatitude { get; set; }
    public string? BottomLongitude { get; set; }
    public string? Easting { get; set; }
    public int Northing { get; set; }
    public string? BottomEasting { get; set; }
    public string? BottomNorthing { get; set; }
    public string? UtmZone { get; set; }
    public string? Elipsoid { get; set; }
    public string? Datum { get; set; }
    public string? SourceOfCoordinate { get; set; }
    public string? VerficationStatusOfCoordinate { get; set; }
    public string? WellLocation { get; set; }
    public string? DrillingMeasuredFrom { get; set; }
    public string? LogMeasuredFrom { get; set; }
    public string? PermanentDatum { get; set; }
    public string? Kb { get; set; }
    public string? Df { get; set; }
    public string? Rt { get; set; }
    public string? Gl { get; set; }
    public string? DrillingBottomDepthTvd { get; set; }
    public string? DrillingBottomDepthMd { get; set; }
    public string? DrillingBottomDepthTvdss { get; set; }
    public string? LoggingBottomDepthMd { get; set; }
    public string? UnitMeasurementMeterFeet { get; set; }
    public string? RigName { get; set; }
    public string? SpudDate { get; set; }
    public string? FinalDrillTotalDepthDate { get; set; }
    public string? CompletionDate { get; set; }
    public string? AbandonmentDate { get; set; }
    public string? DrillingCompany { get; set; }
    public string? LoggingCompany { get; set; }
    public string? BoreholeProfile { get; set; }
    public string? RecordedBy { get; set; }
    public string? WitnessedBy { get; set; }
    public string? WellStatus { get; set; }
    public string? SeismicReference { get; set; }
    public DateTime? RowCreated { get; set; }
}

模型构建器代码

modelBuilder.Entity<VerTempWellGeneralInformation>(entity =>
{
    entity.ToTable("VER_TEMP_WELL__GENERAL_INFORMATION", "db_owner");

    entity.Property(e => e.AbandonmentDate)
        .HasMaxLength(512)
        .IsUnicode(false)
        .HasColumnName("Abandonment_Date");

    entity.Property(e => e.AliasName)
        .HasMaxLength(512)
        .IsUnicode(false)
        .HasColumnName("Alias_Name");

    entity.Property(e => e.Basin)
        .HasMaxLength(512)
        .IsUnicode(false);

    entity.Property(e => e.BoreholeName)
        .HasMaxLength(512)
        .IsUnicode(false)
        .HasColumnName("Borehole_Name");

    entity.Property(e => e.BoreholeProfile)
        .HasMaxLength(512)
        .IsUnicode(false)
        .HasColumnName("Borehole_profile");

    entity.Property(e => e.BoreholeShortName)
        .HasMaxLength(512)
        .IsUnicode(false)
        .HasColumnName("Borehole_Short_Name");

    entity.Property(e => e.BottomEasting)
        .HasMaxLength(512)
        .IsUnicode(false)
        .HasColumnName("Bottom_Easting");

    entity.Property(e => e.BottomLatitude)
        .HasMaxLength(512)
        .IsUnicode(false)
        .HasColumnName("Bottom_Latitude");

    entity.Property(e => e.BottomLongitude)
        .HasMaxLength(512)
        .IsUnicode(false)
        .HasColumnName("Bottom_Longitude");

    entity.Property(e => e.BottomNorthing)
        .HasMaxLength(512)
        .IsUnicode(false)
        .HasColumnName("Bottom_Northing");

    entity.Property(e => e.CompletionDate)
        .HasMaxLength(512)
        .IsUnicode(false)
        .HasColumnName("Completion_Date");

    entity.Property(e => e.Country)
        .HasMaxLength(512)
        .IsUnicode(false);

    entity.Property(e => e.County)
        .HasMaxLength(512)
        .IsUnicode(false);

    entity.Property(e => e.Datum)
        .HasMaxLength(512)
        .IsUnicode(false)
        .HasColumnName("DATUM");

    entity.Property(e => e.Df)
        .HasMaxLength(512)
        .IsUnicode(false)
        .HasColumnName("DF");

    entity.Property(e => e.District)
        .HasMaxLength(512)
        .IsUnicode(false);

    entity.Property(e => e.DrillingBottomDepthMd)
        .HasMaxLength(512)
        .IsUnicode(false)
        .HasColumnName("Drilling_Bottom_Depth_MD");

    entity.Property(e => e.DrillingBottomDepthTvd)
        .HasMaxLength(512)
        .IsUnicode(false)
        .HasColumnName("Drilling_Bottom_Depth_TVD");

    entity.Property(e => e.DrillingBottomDepthTvdss)
        .HasMaxLength(512)
        .IsUnicode(false)
        .HasColumnName("Drilling_Bottom_Depth_TVDSS");

    entity.Property(e => e.DrillingCompany)
        .HasMaxLength(512)
        .IsUnicode(false)
        .HasColumnName("Drilling_Company");

    entity.Property(e => e.DrillingMeasuredFrom)
        .HasMaxLength(512)
        .IsUnicode(false)
        .HasColumnName("Drilling_Measured_from");

    entity.Property(e => e.Easting)
        .HasMaxLength(512)
        .IsUnicode(false);

    entity.Property(e => e.Elipsoid)
        .HasMaxLength(512)
        .IsUnicode(false)
        .HasColumnName("ELIPSOID");

    entity.Property(e => e.Field)
        .HasMaxLength(512)
        .IsUnicode(false);

    entity.Property(e => e.FinalDrillTotalDepthDate)
        .HasMaxLength(512)
        .IsUnicode(false)
        .HasColumnName("Final_Drill_Total_Depth_Date");

    entity.Property(e => e.Gl)
        .HasMaxLength(512)
        .IsUnicode(false)
        .HasColumnName("GL");

    entity.Property(e => e.IdPepmaps)
        .HasMaxLength(512)
        .IsUnicode(false)
        .HasColumnName("ID_PEPMAPS");

    entity.Property(e => e.Kb)
        .HasMaxLength(512)
        .IsUnicode(false)
        .HasColumnName("KB");

    entity.Property(e => e.Latitude)
        .HasMaxLength(512)
        .IsUnicode(false);

    entity.Property(e => e.LocationName)
        .HasMaxLength(512)
        .IsUnicode(false)
        .HasColumnName("Location_Name");

    entity.Property(e => e.LogMeasuredFrom)
        .HasMaxLength(512)
        .IsUnicode(false)
        .HasColumnName("Log_Measured_from");

    entity.Property(e => e.LoggingBottomDepthMd)
        .HasMaxLength(512)
        .IsUnicode(false)
        .HasColumnName("Logging_bottom_depth_MD");

    entity.Property(e => e.LoggingCompany)
        .HasMaxLength(512)
        .IsUnicode(false)
        .HasColumnName("Logging_Company");

    entity.Property(e => e.Longitude)
        .HasMaxLength(512)
        .IsUnicode(false);

    entity.Property(e => e.No)
        .HasMaxLength(512)
        .IsUnicode(false);

    entity.Property(e => e.Northing)
        .HasMaxLength(512)
        .IsUnicode(false);

    entity.Property(e => e.OperatorCompany)
        .HasMaxLength(512)
        .IsUnicode(false)
        .HasColumnName("Operator_Company");

    entity.Property(e => e.OriginalOperatorCompany)
        .HasMaxLength(512)
        .IsUnicode(false)
        .HasColumnName("Original_Operator_Company");

    entity.Property(e => e.OriginalOwnerCompany)
        .HasMaxLength(512)
        .IsUnicode(false)
        .HasColumnName("Original_Owner_Company");

    entity.Property(e => e.OwnerCompany)
        .HasMaxLength(512)
        .IsUnicode(false)
        .HasColumnName("Owner_Company");

    entity.Property(e => e.PermanentDatum)
        .HasMaxLength(512)
        .IsUnicode(false)
        .HasColumnName("Permanent_Datum");

    entity.Property(e => e.Province)
        .HasMaxLength(512)
        .IsUnicode(false);

    entity.Property(e => e.RecordedBy)
        .HasMaxLength(512)
        .IsUnicode(false)
        .HasColumnName("Recorded_by");

    entity.Property(e => e.RigName)
        .HasMaxLength(512)
        .IsUnicode(false)
        .HasColumnName("Rig_Name");

    entity.Property(e => e.RowCreated)
        .HasColumnType("datetime")
        .HasColumnName("ROW_CREATED");

    entity.Property(e => e.Rt)
        .HasMaxLength(512)
        .IsUnicode(false)
        .HasColumnName("RT");

    entity.Property(e => e.SeismicReference)
        .HasMaxLength(512)
        .IsUnicode(false)
        .HasColumnName("Seismic_Reference");

    entity.Property(e => e.SourceOfCoordinate)
        .HasMaxLength(512)
        .IsUnicode(false)
        .HasColumnName("Source_of_Coordinate");

    entity.Property(e => e.SpudDate)
        .HasMaxLength(512)
        .IsUnicode(false)
        .HasColumnName("Spud_Date");

    entity.Property(e => e.Structure)
        .HasMaxLength(512)
        .IsUnicode(false);

    entity.Property(e => e.UniqueBoreholeId)
        .HasMaxLength(512)
        .IsUnicode(false)
        .HasColumnName("Unique_Borehole_ID");

    entity.Property(e => e.UniqueWellId)
        .HasMaxLength(512)
        .IsUnicode(false)
        .HasColumnName("Unique_Well_ID");

    entity.Property(e => e.UnitMeasurementMeterFeet)
        .HasMaxLength(512)
        .IsUnicode(false)
        .HasColumnName("Unit_measurement_METER_FEET");

    entity.Property(e => e.UtmZone)
        .HasMaxLength(512)
        .IsUnicode(false)
        .HasColumnName("UTM_ZONE");

    entity.Property(e => e.VerficationStatusOfCoordinate)
        .HasMaxLength(512)
        .IsUnicode(false)
        .HasColumnName("Verfication_Status_of_Coordinate");

    entity.Property(e => e.Well)
        .HasMaxLength(512)
        .IsUnicode(false);

    entity.Property(e => e.WellLocation)
        .HasMaxLength(512)
        .IsUnicode(false)
        .HasColumnName("Well_Location");

    entity.Property(e => e.WellName)
        .HasMaxLength(512)
        .IsUnicode(false)
        .HasColumnName("Well_Name");

    entity.Property(e => e.WellShortName)
        .HasMaxLength(512)
        .IsUnicode(false)
        .HasColumnName("Well_Short_Name");

    entity.Property(e => e.WellStatus)
        .HasMaxLength(512)
        .IsUnicode(false)
        .HasColumnName("Well_Status");

    entity.Property(e => e.WitnessedBy)
        .HasMaxLength(512)
        .IsUnicode(false)
        .HasColumnName("Witnessed_by");
});

问题原因

  1. 实体映射不匹配:模型构建器中配置的是VerTempWellGeneralInformation实体,但实际使用的模型是VerDummyWellGeneralInformation,导致EF无法将模型正确映射到数据库表VER_TEMP_WELL__GENERAL_INFORMATION。
  2. 字段类型不兼容:模型中Northing字段定义为非可空的int类型,但数据库中该字段允许存储NULL值,当EF尝试将NULL值转换为非可空int时,抛出SqlNullValueException。

解决方案

方案1:修正实体映射配置

将模型构建器中的VerTempWellGeneralInformation替换为VerDummyWellGeneralInformation,确保实体与数据库表正确绑定:

modelBuilder.Entity<VerDummyWellGeneralInformation>(entity =>
{
    // 保留原有的属性配置内容
    entity.ToTable("VER_TEMP_WELL__GENERAL_INFORMATION", "db_owner");
    // ... 其余属性配置不变
});

方案2:修正字段可空性

如果数据库中Northing字段确实存在NULL值,将模型中的Northing字段改为可空类型:

public int? Northing { get; set; }

或者清理数据库中该字段的NULL值,确保所有记录都有有效数值。

内容的提问来源于stack exchange,提问作者KimKim

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 21:15:40