System.Data.SqlTypes.SqlNullValueException我从net core 3.1迁移到net5.0后
System.Data.SqlTypes.SqlNullValueException after I migrated to net5.0 from netcore 3.1
我在其中一项提取服务中遇到此错误
System.Data.SqlTypes.SqlNullValueException
HResult=0x80131931
Message=Data is Null. This method or property cannot be called on Null
values. Source=Microsoft.Data.SqlClient StackTrace: at
Microsoft.Data.SqlClient.SqlBuffer.get_Int32() at
Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable1.Enumerator.MoveNext() at System.Collections.Generic.List
1..ctor(IEnumerable1 collection) at System.Linq.Enumerable.ToList[TSource](IEnumerable
1 source) at
Infrastructure.Data.Repository.Base.EfRepository1.List(ISpecification
1
spec) in C:\mydir\master\src\DataTier\SMYLS.Data.Respository\Repository\Base\EfRepository.cs:line
88 at
ApplicationCore.Services.Items.ItemService.SearchItems(ItemSearchDataModel
searchModel) in C:\mydir\master\src\ServiceTier\SMYLS.Services\Items\ItemService.cs:line 202 at
Web.Controllers.Api.InvoiceController.GetItemList(ItemSearchDataModel
searchModel) in C:\mydir\master\src\Web\Controllers\Api\InvoiceController.cs:line 322 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()
This exception was originally thrown at this call stack:
[External Code]
Infrastructure.Data.Repository.Base.EfRepository.List(ApplicationCore.Interfaces.Repository.ISpecification)
in EfRepository.cs
ApplicationCore.Services.Items.ItemService.SearchItems(ApplicationCore.DTOs.Items.ItemSearchDataModel)
in ItemService.cs
Web.Controllers.Api.InvoiceController.GetItemList(ApplicationCore.DTOs.Items.ItemSearchDataModel)
in InvoiceController.cs
[External Code]
这是我迁移到net5.0后发生的。如果我运行我的netcore3.1,完全没有问题。
我研究了这个错误,发现大部分错误是由于您的模型需要一个必需的 属性 但数据库 returns 是一个空值。我检查了我的模型,但它们看起来很好(它们在 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; }
这些是我来自数据库的样本数据
仍然无法弄清楚为什么这在 net5.0 中不起作用
快速浏览一下,我怀疑 ServiceGroup
属性,它具有 ServiceGroup
的 return 类型,但对应于可为 null 的 ServiceGroupId
数据库中的列。我希望 属性 的 return 类型需要是 ServiceGroup?
才能允许空值。
我不确定为什么在 ASP.NET Core 3.x 中没有抛出异常。我怀疑 ASP.NET 核心 5.x 在运行时执行引用类型的可空性方面做得更好,从而提高了对数据模型中长期存在的差异的认识。
我在其中一项提取服务中遇到此错误
System.Data.SqlTypes.SqlNullValueException HResult=0x80131931
Message=Data is Null. This method or property cannot be called on Null values. Source=Microsoft.Data.SqlClient StackTrace: at Microsoft.Data.SqlClient.SqlBuffer.get_Int32() at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable1.Enumerator.MoveNext() at System.Collections.Generic.List
1..ctor(IEnumerable1 collection) at System.Linq.Enumerable.ToList[TSource](IEnumerable
1 source) at Infrastructure.Data.Repository.Base.EfRepository1.List(ISpecification
1 spec) in C:\mydir\master\src\DataTier\SMYLS.Data.Respository\Repository\Base\EfRepository.cs:line 88 at ApplicationCore.Services.Items.ItemService.SearchItems(ItemSearchDataModel searchModel) in C:\mydir\master\src\ServiceTier\SMYLS.Services\Items\ItemService.cs:line 202 at Web.Controllers.Api.InvoiceController.GetItemList(ItemSearchDataModel searchModel) in C:\mydir\master\src\Web\Controllers\Api\InvoiceController.cs:line 322 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()This exception was originally thrown at this call stack: [External Code] Infrastructure.Data.Repository.Base.EfRepository.List(ApplicationCore.Interfaces.Repository.ISpecification) in EfRepository.cs ApplicationCore.Services.Items.ItemService.SearchItems(ApplicationCore.DTOs.Items.ItemSearchDataModel) in ItemService.cs Web.Controllers.Api.InvoiceController.GetItemList(ApplicationCore.DTOs.Items.ItemSearchDataModel) in InvoiceController.cs [External Code]
这是我迁移到net5.0后发生的。如果我运行我的netcore3.1,完全没有问题。 我研究了这个错误,发现大部分错误是由于您的模型需要一个必需的 属性 但数据库 returns 是一个空值。我检查了我的模型,但它们看起来很好(它们在 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; }
这些是我来自数据库的样本数据
仍然无法弄清楚为什么这在 net5.0 中不起作用
快速浏览一下,我怀疑 ServiceGroup
属性,它具有 ServiceGroup
的 return 类型,但对应于可为 null 的 ServiceGroupId
数据库中的列。我希望 属性 的 return 类型需要是 ServiceGroup?
才能允许空值。
我不确定为什么在 ASP.NET Core 3.x 中没有抛出异常。我怀疑 ASP.NET 核心 5.x 在运行时执行引用类型的可空性方面做得更好,从而提高了对数据模型中长期存在的差异的认识。