【问题标题】:System.Data.SqlTypes.SqlNullValueException after I migrated to net5.0 from netcore 3.1System.Data.SqlTypes.SqlNullValueException 在我从 netcore 3.1 迁移到 net5.0 之后
【发布时间】:2021-05-07 10:54:04
【问题描述】:

我的一项提取服务出现此错误

System.Data.SqlTypes.SqlNullValueException HResult=0x80131931
消息=数据为空。无法在 Null 上调用此方法或属性 价值观。源 = Microsoft.Data.SqlClient StackTrace:在 Microsoft.Data.SqlClient.SqlBuffer.get_Int32() 在 Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable1.Enumerator.MoveNext() at System.Collections.Generic.List1..ctor(IEnumerable1 collection) at System.Linq.Enumerable.ToList[TSource](IEnumerable1 源) 在 Infrastructure.Data.Repository.Base.EfRepository1.List(ISpecification1 规范)在 C:\mydir\master\src\DataTier\SMYLS.Data.Respository\Repository\Base\EfRepository.cs:line 88 在 ApplicationCore.Services.Items.ItemService.SearchItems(ItemSearchDataModel searchModel) 在 C:\mydir\master\src\ServiceTier\SMYLS.Services\Items\ItemService.cs:line 202 at Web.Controllers.Api.InvoiceController.GetItemList(ItemSearchDataModel searchModel) 在 C:\mydir\master\src\Web\Controllers\Api\InvoiceController.cs:line 322 at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.SyncActionResultExecutor.Execute(IActionResultTypeMapper 映射器,ObjectMethodExecutor 执行器,对象控制器,Object[] 论据)在 Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeActionMethodAsync() 在 Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(状态& next, Scope& 范围, Object& 状态, Boolean& isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeNextActionFilterAsync()

此异常最初是在此调用堆栈中引发的: [外部代码] Infrastructure.Data.Repository.Base.EfRepository.List(ApplicationCore.Interfaces.Repository.ISpecification) 在 EfRepository.cs ApplicationCore.Services.Items.ItemService.SearchItems(ApplicationCore.DTOs.Items.ItemSearchDataModel) 在 ItemService.cs Web.Controllers.Api.InvoiceController.GetItemList(ApplicationCore.DTOs.Items.ItemSearchDataModel) 在 InvoiceController.cs [外部代码]

这发生在我迁移到 net5.0 之后。如果我运行我的 netcore3.1,完全没有问题。 我已经研究过这个错误,主要发现了当你的模型需要一个必需的属性但数据库返回一个空值时引起的错误。我检查了我的模型,但它们看起来很好(它们在 3.1 上工作没有任何问题)

这些是我的模型属性

public string Name { get; set; }
public string Description { get; set; }
public decimal Cost { get; set; }
public bool Active { get; set; }
public DateTime UpdatedDateUtc { get; set; }
public int UpdatedBy { get; set; }
public int ClinicId { get; set; }
public int? ServiceGroupId {get;set;}
public string ShortCode { get; set; }
public int? IndustryCodeId { get; set; }
public bool Subscription { get; set; }
public bool BlockBill { get; set; }
public decimal? DiscountPercentage { get; set; }
#nullable enable
public string? SubscriptionType { get; set; }
public virtual Clinic Clinic { get; set; }
public virtual SiteUser UpdatedByNavigation { get; set; }
public virtual ServiceGroup ServiceGroup { get; set; }
public virtual IndustryCode IndustryCode { get; set; }
public virtual ICollection<InvoiceItem> InvoiceItem { get; set; }
public virtual ICollection<DoctorPricing> DoctorPricing { get; set; }
public virtual ICollection<DoctorPricingHistory> DoctorPricingHistory { get; set; }

这些是我来自 db 的样本数据

仍然无法弄清楚为什么这在 net5.0 中不起作用

【问题讨论】:

  • 如果ServiceGroup 的属性的返回类型为ServiceGroup?,以说明可空列ServiceGroupId
  • @JeremyCaney 哇,你是对的。您所说的正是解决错误的方法。我已经调试了好几天了。您可以将其发布为答案,以便我将其标记为正确答案吗?非常感谢。
  • 哦,哇,很高兴听到!有时它只需要那双额外的眼睛。我已将我的评论正式化为答案,但我无法就为什么 ASP.NET Core 适合您的模型给出明确的答案。

标签: asp.net-core .net-5 nullable-reference-types asp.net-core-5.0 ef-core-5.0


【解决方案1】:

快速浏览一下,我会怀疑ServiceGroup 属性,它的返回类型为ServiceGroup,但对应于数据库中一个可以为空的ServiceGroupId 列。我希望属性的返回类型需要为 ServiceGroup? 以允许空值。

我不确定为什么在 ASP.NET Core 3.x 中没有引发异常。我怀疑 ASP.NET Core 5.x 在运行时强制引用类型的可空性方面做得更好,从而提高了对数据模型中长期存在的差异的认识。

【讨论】:

    猜你喜欢
    • 2021-09-02
    • 2022-01-04
    • 1970-01-01
    • 2020-06-18
    • 2021-01-15
    • 2020-05-27
    • 2020-04-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多