不可为空 属性 必须包含一个非空值

Non-Nullable Property Must Contain A Non-Null Value

有什么方法可以处理 Visual Studio 中 mac 中的“不可为 Nullable 属性 必须包含非 Null 值”警报。 好吧,我找不到通过从 csproj 项目文件或配置中删除该行来禁用它的方法。 这些警告在我的数据库模型和视图中,我的想法是以最好的方式进行。 应该说我用的是.Net 6.0

提前感谢您花时间阅读这个问题。

我假设您启用了可空引用类型。有多种方法可以解决这个问题。

  1. 将这些属性显式初始化为默认值或已知值。
    public class SomeType
    {
        public string SomeProperty { get; set; } = default!;
    }
    
  2. 禁用整个文件或部分的可为空的引用类型。
    #nullable disable // at the top of the file
    
    #nullable restore // after the block of code you wanted to temporarily disable
    
  3. 为整个项目禁用可为空的引用类型。
    删除或更改项目文件中的 <Nullable> 设置。 (在 .NET 6 中默认启用)

我会尽量坚持#1。如果打算进行转换,请不要理会 #3。