System.Data.SqlTypes.SqlNullValueException after I migrated to net5.0 from netcore 3.1

0

I'm getting this error on one of my fetch services

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.List1..ctor(IEnumerable1 collection) at System.Linq.Enumerable.ToList[TSource](IEnumerable1 source) at Infrastructure.Data.Repository.Base.EfRepository1.List(ISpecification1 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]

This happened after I migrated to net5.0. If I run my netcore3.1, there's no problem at all. I've researched this error and mostly found errors that were caused when your model is expecting a required property but the database returns a null value. I checked my models but they seem fine (they work on 3.1 without any issue)

these are my model properties

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

And these are my sample data from db enter image description here

Still can't figure out why this is not working in net5.0

asp.net-core
.net-5
nullable-reference-types
asp.net-core-5.0
ef-core-5.0
asked on Stack Overflow Feb 3, 2021 by JkAlombro • edited Feb 3, 2021 by Jeremy Caney

1 Answer

2

On quick glance, I’d suspect the ServiceGroup property, which has a return type of ServiceGroup, but corresponds to a nullable ServiceGroupId column in the database. I’d expect that the return type of the property would need to be ServiceGroup? to allow for nulls.

I’m not certain why that wasn’t throwing an exception in ASP.NET Core 3.x. My suspicion is that ASP.NET Core 5.x does a better job of enforcing nullability of reference types at runtime, thus raising awareness of a long-standing discrepancy in the data model.

answered on Stack Overflow Feb 3, 2021 by Jeremy Caney

User contributions licensed under CC BY-SA 3.0