Home > database >  I upgraded from .NET 5 to .NET 6 and now getting SqlNullValueException from a LINQ query
I upgraded from .NET 5 to .NET 6 and now getting SqlNullValueException from a LINQ query

Time:10-24

I just upgraded a project from .NET 5 to the .NET 6 preview and started using Visual Studio 2022. But I'm getting a SqlNullValueException that I wasn't getting before. And it's not making sense to me.

Here is the LINQ query:

var clients = _dbContext.AppUserAppUsers
            .Where(x => x.AppUserParentId == designerId
                        && x.ChildAppUser.AppUserRole == AppUserRole.Client)
            .Include(x => x.ChildAppUser.Profile)
            .Select(x => x.ChildAppUser)
            .AsNoTracking();

return clients.ToList();

The AppUserAppUsers entity is a many-to-many model for AppUsers. I'm using the Table-per-hierarchy pattern So I have AppUserBase abstract class and DesignerModel and ClientModel both extend that class.

public class AppUserAppUser
    {
        public int AppUserParentId { get; set; }

        public int AppUserChildId { get; set; }

        public bool Active { get; set; }

        public AppUserBase ParentAppUser { get; set; }

        public AppUserBase ChildAppUser { get; set; }
    }

Again, all this works beautifully in .NET 5!

The call to ToList() throws this exception:

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.ThrowIfNull()
   at Microsoft.Data.SqlClient.SqlBuffer.get_String()
   at Microsoft.Data.SqlClient.SqlDataReader.GetString(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 Design2WorkroomApi.Repository.AppUserRepository.GetClientsByDesignerId(Int32 designerId) in C:\Users\davew\source\repos\Design2WorkroomApi\Design2WorkroomApi\Repository\AppUserRepository.cs:line 65
   at Design2ClientAPI.Controllers.ClientsController.GetClientsByDesignerId(Int32 designerId) in C:\Users\davew\source\repos\Design2WorkroomApi\Design2WorkroomApi\Controllers\ClientsController.cs:line 56
   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()

If I take out the Include and the Select calls this works fine. But either one and the LINQ statement returns null.

In the .NET 5 version, this same method with the same parameter works just fine. And I have used similar code many times before with no problems.

Obviously, something in .NET 6 has changed to cause this, but I don't know what it is.

Here are my Entity Framework Core packages. I had the same issue before I upgraded these packages.

 <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="6.0.0-rc.2.21480.10" />
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.0-rc.2.21480.5" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.0-rc.2.21480.5" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.0-rc.2.21480.5">

CodePudding user response:

Remove the .Include(), it is not needed in this particular case and it can cause the error which you are getting.

The new query should simply look something like:

var clients = _dbContext.AppUserAppUsers
            .Where(x => x.AppUserParentId == designerId
                        && x.ChildAppUser.AppUserRole == AppUserRole.Client)
            .Select(x => x.ChildAppUser)
            .AsNoTracking();
  • Related