我剛剛將一個專案從 .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:\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()
如果我取出 Include 和 Select 呼叫,這可以正常作業。但是任一和 LINQ 陳述句都回傳 null。
在 .NET 5 版本中,使用相同引數的相同方法可以正常作業。我以前多次使用過類似的代碼,沒有任何問題。
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">
uj5u.com熱心網友回復:
洗掉.Include(),在這種特殊情況下不需要它,它可能會導致您遇到的錯誤。
新查詢應該看起來像:
var clients = _dbContext.AppUserAppUsers
.Where(x => x.AppUserParentId == designerId
&& x.ChildAppUser.AppUserRole == AppUserRole.Client)
.Select(x => x.ChildAppUser)
.AsNoTracking();
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/337111.html
標籤:c# .net linq asp.net-core entity-framework-core
上一篇:形式成為焦點
下一篇:AndroidJavaGoogleAPI應用程式在允許位置訪問時崩潰,但在拒絕位置時沒有問題......?[復制]
