Visual Studio 假定来自 FirstOrDefaultAsync() 的 return 值不能为 null
Visual Studio assumes return value from FirstOrDefaultAsync() cannot be null
我有以下查询。
var railcarInfo = await (from rt in DbContext.RailcarTrips
where rt.WaybillRailcar.RailcarNumber == clm.RailcarNumber &&
rt.WaybillRailcar.Waybill.CreateDate <= clm.SightingDate
orderby rt.WaybillRailcar.Waybill.CreateDate descending
select new
{
RailcarTrip = rt,
WaybillCreateDate = rt.WaybillRailcar.Waybill.CreateDate,
IsLoaded = rt.WaybillRailcar.Weight > 0
})
.AsNoTracking()
.FirstOrDefaultAsync();
但出于某种原因,Visual Studio 决定 'railcarInfo' 在此处 不为空,紧接着此查询。
FirstOrDefaultAsync()
的要点是它 returns 集合中的第一项,如果集合为空则为 null。它在这里绝对可以为空。谁能看出为什么 Visual Studio 似乎在这里感到困惑?
如docs中提到的关于此功能和 EF:
Prior to EF Core 6.0, the public API surface wasn't annotated for
nullability (the public API was "null-oblivious"), making it sometimes
awkward to use when the NRT feature is turned on. This notably
includes the async LINQ operators exposed by EF Core, such as
FirstOrDefaultAsync. The public API is fully annotated for nullability
starting with EF Core 6.0.
所以您可能使用 < 6.0 的 EF 版本
我有以下查询。
var railcarInfo = await (from rt in DbContext.RailcarTrips
where rt.WaybillRailcar.RailcarNumber == clm.RailcarNumber &&
rt.WaybillRailcar.Waybill.CreateDate <= clm.SightingDate
orderby rt.WaybillRailcar.Waybill.CreateDate descending
select new
{
RailcarTrip = rt,
WaybillCreateDate = rt.WaybillRailcar.Waybill.CreateDate,
IsLoaded = rt.WaybillRailcar.Weight > 0
})
.AsNoTracking()
.FirstOrDefaultAsync();
但出于某种原因,Visual Studio 决定 'railcarInfo' 在此处 不为空,紧接着此查询。
FirstOrDefaultAsync()
的要点是它 returns 集合中的第一项,如果集合为空则为 null。它在这里绝对可以为空。谁能看出为什么 Visual Studio 似乎在这里感到困惑?
如docs中提到的关于此功能和 EF:
Prior to EF Core 6.0, the public API surface wasn't annotated for nullability (the public API was "null-oblivious"), making it sometimes awkward to use when the NRT feature is turned on. This notably includes the async LINQ operators exposed by EF Core, such as FirstOrDefaultAsync. The public API is fully annotated for nullability starting with EF Core 6.0.
所以您可能使用 < 6.0 的 EF 版本