5

I am using jet brains Rider and Resharper gives an error when i dont declare objects like Organization as nullable. enter image description here

Actually, it should be nullable, since i might not include it in the EFCore query.

Now I've set it to public Organization? Organization { get; set; }

The problem I'm facing is this: enter image description here

What is the best way to solve this warning? What is the best practice here?

5
  • 1
    Related: github.com/dotnet/efcore/issues/17212 Commented Jan 24, 2024 at 16:32
  • 1
    Post your code as text. The warning doesn't appear in the Include operations. The actual code is hidden by a popup Commented Jan 24, 2024 at 16:48
  • @JohnathanBarclay maybe not - these aren't DbSets and the actual warning is raised in the Where operator which is hidden by a popup. For all we know this may be a valid warning Commented Jan 24, 2024 at 16:49
  • @PanagiotisKanavos There's also a warning in ThenInclude. Commented Jan 24, 2024 at 16:50
  • 2
    All the more reason to post both code and warnings as text, not images Commented Jan 24, 2024 at 16:50

1 Answer 1

18

The warning is safe to ignore since the expression will be translated by EF core to an SQL Query, and won't be actually evaluated by the .NET runtime (so no NullReferenceException would be thrown).

So the ! (null-forgiving) operator can be used to silence the warning as in

Where(g => g.Organization!. /* rest of the expression here*/ )
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.