简体   繁体   English

如何解决错误“System.InvalidCastException - 列包含 NULL 数据”

[英]How to solve error "System.InvalidCastException - column contains NULL data"

When I run the application and search I got this error当我运行应用程序并进行搜索时,出现此错误

System.InvalidCastException - column contains NULL data System.InvalidCastException - 列包含 NULL 条数据

at Oracle.ManagedDataAccess.Client.OracleDataReader.GetDecimal在 Oracle.ManagedDataAccess.Client.OracleDataReader.GetDecimal

This is my code DB repository code:这是我的代码数据库存储库代码:

public List<LabResult> Search(string term)
{
    return db.LabResults
             .Where(a => a.PatientNo.ToString() == term)
             .ToList(); // error on this line 
}

This is the view markup:这是视图标记:

@model IEnumerable<OracleHIS.Models.LabResult>

@{
    ViewData["Title"] = "Index";
}

<table class="table">
    <thead>
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.PatientNo)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.LabOrderNo)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.PatientNameE)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.LongForiegnDesc)
            </th>
           <th>
                @Html.DisplayNameFor(model => model.ServNumResult)
            </th>
        </tr>
    </thead>
    <tbody>
        @if (Model != null)
        {
            foreach (var item in Model) 
            {
            <tr>
              <td>
                @Html.DisplayFor(modelItem => item.PatientNo)
              </td>
              <td>
                 @Html.DisplayFor(modelItem => item.LabOrderNo)
              </td>
              <td>
                 @Html.DisplayFor(modelItem => item.PatientNameE)
              </td>
              <td>
                 @Html.DisplayFor(modelItem => item.LongForeignDesc)
              </td>
              <td>
                 @Html.DisplayFor(modelItem => item.ServNumResult)
              </td>
              <td>
                @Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
              </td>
           </tr>
        }
    }
    </tbody>
</table>

And this is the model class:这是 model class:

namespace OracleHIS.Models
{
    public partial class LabResult
    {
        public decimal PatientNo { get; set; } 
        public decimal LabOrderNo { get; set; }
        public string PatientNameE { get; set; } = null!;
        public string LongForiegnDesc { get; set; } = null!;
        public decimal ServNumResult { get; set; }
    }
}

I found this solution我找到了这个解决方案

https://stackoverflow.com/questions/26024722/handle-null-values-when-reading-through-oracledatareader

but where I will use the IsDBNull() in my code?但是我将在我的代码中的什么地方使用IsDBNull()呢?

OracleDataReader provides a IsDBNull() method. OracleDataReader提供了一个IsDBNull()方法。

this is the Model in DBset context its a VIEW not TABLE include columns from multiple tables:这是 DBset 上下文中的 Model 它是 VIEW 而不是 TABLE,包括来自多个表的列:

protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.HasDefaultSchema("TRNGKAASH")
                .UseCollation("USING_NLS_COMP");

            modelBuilder.Entity<LabResult>(entity =>
            {
                entity.HasNoKey();

                entity.ToView("LAB_RESULTS");

                entity.Property(e => e.AbnormalFlag)
                    .HasColumnType("NUMBER")
                    .HasColumnName("ABNORMAL_FLAG");

                entity.Property(e => e.ApprovingDateG)
                    .HasColumnType("DATE")
                    .HasColumnName("APPROVING_DATE_G");

                entity.Property(e => e.CancelBy)
                    .HasMaxLength(8)
                    .IsUnicode(false)
                    .HasColumnName("CANCEL_BY");

                entity.Property(e => e.CancelDateG)
                    .HasColumnType("DATE")
                    .HasColumnName("CANCEL_DATE_G");

                entity.Property(e => e.CancelDateH)
                    .HasMaxLength(8)
                    .IsUnicode(false)
                    .HasColumnName("CANCEL_DATE_H");

                entity.Property(e => e.CancelReason)
                    .HasPrecision(6)
                    .HasColumnName("CANCEL_REASON");

                entity.Property(e => e.DateOfBirth)
                    .HasPrecision(8)
                    .HasColumnName("DATE_OF_BIRTH");

                entity.Property(e => e.EndResult)
                    .HasPrecision(6)
                    .HasColumnName("END_RESULT");

                entity.Property(e => e.EventNo)
                    .HasPrecision(4)
                    .HasColumnName("EVENT_NO");

                entity.Property(e => e.GramStain)
                    .HasMaxLength(3000)
                    .IsUnicode(false)
                    .HasColumnName("GRAM_STAIN");

                entity.Property(e => e.GroupNo)
                    .HasPrecision(6)
                    .HasColumnName("GROUP_NO");

                entity.Property(e => e.HeparinFlag)
                    .HasPrecision(1)
                    .HasColumnName("HEPARIN_FLAG");

                entity.Property(e => e.HospitalNo)
                    .HasMaxLength(10)
                    .IsUnicode(false)
                    .HasColumnName("HOSPITAL_NO");

                entity.Property(e => e.InitDiagnisis)
                    .HasMaxLength(300)
                    .IsUnicode(false)
                    .HasColumnName("INIT_DIAGNISIS");

                entity.Property(e => e.LabNo)
                    .HasPrecision(6)
                    .HasColumnName("LAB_NO");

                entity.Property(e => e.LabOrderNo)
                    .HasPrecision(12)
                    .HasColumnName("LAB_ORDER_NO");

                entity.Property(e => e.LastUpdateDate)
                    .HasColumnType("DATE")
                    .HasColumnName("LAST_UPDATE_DATE");

                entity.Property(e => e.LastUpdateTransaction)
                    .HasMaxLength(1)
                    .IsUnicode(false)
                    .HasColumnName("LAST_UPDATE_TRANSACTION");

                entity.Property(e => e.LastUpdateUser)
                    .HasMaxLength(8)
                    .IsUnicode(false)
                    .HasColumnName("LAST_UPDATE_USER");

                entity.Property(e => e.LongForiegnDesc)
                    .HasMaxLength(40)
                    .IsUnicode(false)
                    .HasColumnName("LONG_FORIEGN_DESC");

                entity.Property(e => e.MachineId)
                    .HasColumnType("NUMBER")
                    .HasColumnName("MACHINE_ID");

                entity.Property(e => e.MedicalCheck)
                    .HasPrecision(1)
                    .HasColumnName("MEDICAL_CHECK");

                entity.Property(e => e.MrMerge)
                    .HasPrecision(12)
                    .HasColumnName("MR_MERGE");

                entity.Property(e => e.Nationality)
                    .HasPrecision(6)
                    .HasColumnName("NATIONALITY");

                entity.Property(e => e.PanicFlag)
                    .HasColumnType("NUMBER")
                    .HasColumnName("PANIC_FLAG");

                entity.Property(e => e.PatientCategory)
                    .HasPrecision(6)
                    .HasColumnName("PATIENT_CATEGORY");

                entity.Property(e => e.PatientHospital)
                    .HasMaxLength(10)
                    .IsUnicode(false)
                    .HasColumnName("PATIENT_HOSPITAL");

                entity.Property(e => e.PatientNameA)
                    .HasMaxLength(150)
                    .IsUnicode(false)
                    .HasColumnName("PATIENT_NAME_A");

                entity.Property(e => e.PatientNameE)
                    .HasMaxLength(150)
                    .IsUnicode(false)
                    .HasColumnName("PATIENT_NAME_E");

                entity.Property(e => e.PatientNo)
                    .HasPrecision(12)
                    .HasColumnName("PATIENT_NO");

                entity.Property(e => e.PatientSourceInd)
                    .HasPrecision(6)
                    .HasColumnName("PATIENT_SOURCE_IND");

                entity.Property(e => e.PrioFlag)
                    .HasPrecision(6)
                    .HasColumnName("PRIO_FLAG");

                entity.Property(e => e.ProvidingResource)
                    .HasPrecision(6)
                    .HasColumnName("PROVIDING_RESOURCE");

                entity.Property(e => e.Reason)
                    .HasMaxLength(300)
                    .IsUnicode(false)
                    .HasColumnName("REASON");

                entity.Property(e => e.RefSourceNo)
                    .HasMaxLength(10)
                    .IsUnicode(false)
                    .HasColumnName("REF_SOURCE_NO");

                entity.Property(e => e.RefType)
                    .HasPrecision(6)
                    .HasColumnName("REF_TYPE");

                entity.Property(e => e.ResultNotes)
                    .IsUnicode(false)
                    .HasColumnName("RESULT_NOTES");

                entity.Property(e => e.SampleCollectedBy)
                    .HasPrecision(5)
                    .HasColumnName("SAMPLE_COLLECTED_BY");

                entity.Property(e => e.SampleCollectedDateG)
                    .HasColumnType("DATE")
                    .HasColumnName("SAMPLE_COLLECTED_DATE_G");

                entity.Property(e => e.SampleCollectedDateH)
                    .HasMaxLength(8)
                    .IsUnicode(false)
                    .HasColumnName("SAMPLE_COLLECTED_DATE_H");

                entity.Property(e => e.SampleNo)
                    .HasPrecision(12)
                    .HasColumnName("SAMPLE_NO");

                entity.Property(e => e.SampleNote)
                    .HasMaxLength(300)
                    .IsUnicode(false)
                    .HasColumnName("SAMPLE_NOTE");

                entity.Property(e => e.SampleReceivedDateG)
                    .HasColumnType("DATE")
                    .HasColumnName("SAMPLE_RECEIVED_DATE_G");

                entity.Property(e => e.SampleReceivedDateH)
                    .HasMaxLength(8)
                    .IsUnicode(false)
                    .HasColumnName("SAMPLE_RECEIVED_DATE_H");

                entity.Property(e => e.SampleRecievedBy)
                    .HasMaxLength(8)
                    .IsUnicode(false)
                    .HasColumnName("SAMPLE_RECIEVED_BY");

                entity.Property(e => e.SampleType)
                    .HasPrecision(6)
                    .HasColumnName("SAMPLE_TYPE");

                entity.Property(e => e.ServCancelBy)
                    .HasMaxLength(8)
                    .IsUnicode(false)
                    .HasColumnName("SERV_CANCEL_BY");

                entity.Property(e => e.ServCancelDateG)
                    .HasColumnType("DATE")
                    .HasColumnName("SERV_CANCEL_DATE_G");

                entity.Property(e => e.ServCancelDateH)
                    .HasMaxLength(8)
                    .IsUnicode(false)
                    .HasColumnName("SERV_CANCEL_DATE_H");

                entity.Property(e => e.ServCancelReason)
                    .HasColumnType("NUMBER")
                    .HasColumnName("SERV_CANCEL_REASON");

                entity.Property(e => e.ServNo)
                    .HasPrecision(6)
                    .HasColumnName("SERV_NO");

                entity.Property(e => e.ServNumResult)
                    .HasColumnType("NUMBER")
                    .HasColumnName("SERV_NUM_RESULT");

                entity.Property(e => e.ServRequestDateG)
                    .HasColumnType("DATE")
                    .HasColumnName("SERV_REQUEST_DATE_G");

                entity.Property(e => e.ServRequestDateH)
                    .HasMaxLength(8)
                    .IsUnicode(false)
                    .HasColumnName("SERV_REQUEST_DATE_H");

                entity.Property(e => e.ServRequestDoctorName)
                    .HasMaxLength(150)
                    .IsUnicode(false)
                    .HasColumnName("SERV_REQUEST_DOCTOR_NAME");

                entity.Property(e => e.ServRequestDoctorNo)
                    .HasPrecision(5)
                    .HasColumnName("SERV_REQUEST_DOCTOR_NO");

                entity.Property(e => e.ServRequestUserId)
                    .HasMaxLength(8)
                    .IsUnicode(false)
                    .HasColumnName("SERV_REQUEST_USER_ID");

                entity.Property(e => e.ServTextResult)
                    .HasMaxLength(500)
                    .IsUnicode(false)
                    .HasColumnName("SERV_TEXT_RESULT");

                entity.Property(e => e.ServType)
                    .HasPrecision(6)
                    .HasColumnName("SERV_TYPE");

                entity.Property(e => e.Sex)
                    .HasPrecision(1)
                    .HasColumnName("SEX");

                entity.Property(e => e.SpecialCase)
                    .HasPrecision(6)
                    .HasColumnName("SPECIAL_CASE");
            });

And this is the VIEW declaration in SQL:这是 SQL 中的 VIEW 声明:

在此处输入图像描述

在此处输入图像描述

I opened view error details and this is the details:我打开查看错误详细信息,这是详细信息:

 at Oracle.ManagedDataAccess.Client.OracleDataReader.GetInt32(Int32 i)
   at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.Enumerator.MoveNext()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at OracleHIS.Models.Repository.LabDbRepository.Search(String term) in D:\HIS\OracleHIS\OracleHIS\Models\Repository\LabDbRepository.cs:line 59
   at OracleHIS.Controllers.LabController.Search(String term) in D:\HIS\OracleHIS\OracleHIS\Controllers\LabController.cs:line 59
   at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.SyncActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeActionMethodAsync()
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeNextActionFilterAsync()

Your model should match your table/view types including the nullability.您的 model 应该与您的表/视图类型匹配,包括可空性。

As the table structure shows - all columns can contain null 's so you need to mark all properties that are value types (ie decimal s, int s, DateTime s, etc.) as nullable value types , as you done with decimal 's (note that actual exception has changed the problem datatype after changing decimal s to descimal? ).如表结构所示 - 所有列都可以包含null ,因此您需要将所有属性值类型(即decimal s、 int s、 DateTime s 等)标记为可为null 的值类型,就像您对decimal的所做的那样(请注意,在将decimal s 更改为 decimal 之后,实际异常已经更改了问题数据类型descimal? )。

If you have nullable reference types enabled in your project you may want to mark reference type properties too.如果您在项目中启用了可空引用类型,您可能还想标记引用类型属性。

Change the PatientNo to be:将 PatientNo 更改为:

public decimal? PatientNo { get; set; }

also Don't use.ToString() in where clause也不要在 where 子句中使用.ToString()

    public List<LabResult> Search(string term)
    {
        var decm = Convert.ToDecimal(term)

        return db.LabResults
                 .Where(a => a.PatientNo == decm)
                 .ToList();
    }

It is clear that one of the rows have a null for one of the decimal values.很明显,其中一行的十进制值之一为 null。 To confirm, execute the following sql statement:要确认,请执行以下 sql 语句:

Select * from labresults where PatientNo is null or LabOrderNo is null or ServNumResult is null

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM