简体   繁体   English

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

[英]Non-Nullable Property Must Contain A Non-Null Value

Is there any way to handle the "Non-Nullable Property Must Contain A Non-Null Value" alert in Visual Studio for mac.有没有办法在 Visual Studio for mac 中处理“非空属性必须包含非空值”警报。 Well, I can't find a way to disable it by removing the line from the csproj project file or configuration.好吧,我找不到通过从 csproj 项目文件或配置中删除该行来禁用它的方法。 These warnings are in my database models and views and the idea is to proceed in the best way.这些警告在我的数据库模型和视图中,我们的想法是以最好的方式进行。 It should be said that I am using.Net 6.0应该说我用的是.Net 6.0

Thank you in advance for taking the time to read this question.提前感谢您抽出宝贵时间阅读此问题。

I'm assuming you have nullable reference types enabled.我假设您启用了可为空的引用类型。 There's a number of ways you can deal with this.有很多方法可以解决这个问题。

  1. Explicitly initialize those properties to the default or a known value either at the property declaration directly or within a constructor.直接在属性声明处或在构造函数中将这些属性显式初始化为默认值或已知值。
     public class SomeType { public string SomeProperty { get; set; } = default;; }
  2. Disable nullable reference types for the entire file or section.禁用整个文件或部分的可为空的引用类型。
     #nullable disable // at the top of the file #nullable restore // after the block of code you wanted to temporarily disable
  3. Disable nullable reference types for the entire project.禁用整个项目的可为空的引用类型。
    Remove or change the <Nullable> setting in your project file.删除或更改项目文件中的<Nullable>设置。 (It defaults to enabled in .NET 6) (在 .NET 6 中默认启用)

I would try to stick to #1 exclusively.我会尽量坚持#1。 Leave #3 alone if the intention is to make the transition.如果打算进行过渡,请不要理会#3。

I came across the same situation, and found this article: Nullable reference types in C# it can enlighten the OP's doubt.我遇到了同样的情况,发现这篇文章: Nullable reference types in C#它可以启发OP的疑问。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 不可为空的属性必须包含关于 EF 关系的非空值警告 - Non-nullable property must contain a non-null value warning on EF relationships 退出构造函数时,不可为空的属性必须包含一个非空值。 考虑将属性声明为可为空 - Non-nullable property must contain a non-null value when exiting constructor. Consider declaring the property as nullable "“退出构造函数时不可为空的事件必须包含非空值”" - "Non-nullable event must contain a non-null value when exiting constructor" 为什么我会收到这些奇怪的 C# 8 “可空引用类型”警告:在退出构造函数之前,不可空字段必须包含非空值。? - Why am I getting these strange C# 8 “nullable reference types” warnings: Non-nullable field must contain a non-null value before exiting constructor.? 不可为空的属性为null - Non-nullable property is null 类型必须是不可为空的值 - The type must be a non-nullable value 将 null 文字或可能的 null 值转换为不可空类型 - Converting null literal or possible null value to non-nullable type 无法将 null 转换为“bool”,因为它是不可为 null 的值类型 - Cannot convert null to 'bool' because it is a non-nullable value type 实体框架5 DbUpdateException:非可空成员的空值 - Entity Framework 5 DbUpdateException: Null value for non-nullable member 具有非空值类型属性的对象上的LINQ DefaultIfEmpty问题 - LINQ DefaultIfEmpty issue on object with non-nullable value type property
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM