Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Visual Studio assumes return value from FirstOrDefaultAsync() cannot be null #26485

Closed
SoftCircuits opened this issue Oct 28, 2021 · 2 comments
Closed

Comments

@SoftCircuits
Copy link

I have the following query.

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

But for some reason, Visual Studio decides that 'railcarInfo' is not null here immediately following this query.

image

The point of FirstOrDefaultAsync() is that it returns the first item in a collection, or null if the collection is empty. It could definitely be null here. Seems Visual Studio got confused?

Include provider and version information

Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 5.0
Operating system: Windows 11
IDE: Visual Studio 2019 16.11.5

@roji
Copy link
Member

roji commented Oct 28, 2021

Duplicate of #19007

@roji roji marked this as a duplicate of #19007 Oct 28, 2021
@roji
Copy link
Member

roji commented Oct 28, 2021

@SoftCircuits the FirstOrDefaultAsync operator is part of EF Core, and EF Core wasn't annotated for nullable reference types. Try EF Core 6.0 RC2, it should work there.

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants