我从.net 5升级到.net 6,现在从LINQ查询中获得SqlNullValueException



我刚刚将一个项目从。net 5升级到。net 6预览版,并开始使用Visual Studio 2022。但我得到了一个之前没有的SqlNullValueException。这对我来说没有意义。

下面是LINQ查询:

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();

AppUserAppUsers实体是AppUsers的多对多模型。我用的是Table-per-hierarchy模式所以我有AppUserBase抽象类DesignerModel和ClientModel都扩展了那个类

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

同样,所有这些在。net 5中工作得很好!

调用ToList()会抛出以下异常:

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:UsersdavewsourcereposDesign2WorkroomApiDesign2WorkroomApiRepositoryAppUserRepository.cs:line 65
at Design2ClientAPI.Controllers.ClientsController.GetClientsByDesignerId(Int32 designerId) in C:UsersdavewsourcereposDesign2WorkroomApiDesign2WorkroomApiControllersClientsController.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()

如果我拿出包含和选择调用,这工作得很好。但是任何一个和LINQ语句都返回null

在。net 5版本中,这个带有相同参数的相同方法工作得很好。我以前用过很多次类似的代码,没有任何问题。

显然,在。net 6中有什么东西改变了导致这个,但我不知道是什么。

这是我的实体框架核心包。我在升级这些包之前也遇到了同样的问题。

<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">

删除.Include(),在这种特殊情况下不需要它,它可能导致您得到的错误。

新的查询应该看起来像这样:

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

相关内容

  • 没有找到相关文章

最新更新