如何修复我的项目中的所有不可为空的警告

how to fix all Non-nullble warnings in my project

我有 Asp.Net 核心项目

 <Nullable>enable</Nullable>

我添加了 Identity 并收到了 195 多个警告。我如何在不自己重写所有代码的情况下解决这个问题?

来自 Microsoft 文档:

https://docs.microsoft.com/en-us/dotnet/csharp/nullable-migration-strategies

Nullable reference types enable you to declare if variables of a reference type should or shouldn't be assigned a null value. The compiler's static analysis and warnings when your code might dereference null are the most important benefit of this feature. Once enabled, the compiler generates warnings that help you avoid throwing a System.NullReferenceException when your code runs.

If your codebase is relatively small, you can turn on the feature in your project, address warnings, and enjoy the benefits of the improved diagnostics. Larger codebases may require a more structured approach to address warnings over time, enabling the feature for some as you address warnings in different types or files.

“警告”通常是“好事”。[​​=13=]

但是,如果您不准备更新您的代码库,and/or 您不相信这些警告中的大部分是有效的(这完全有可能),那么您应该删除 <Nullable>enable</Nullable>来自您的项目设置。

请务必查看所有这些建议: