[英].NET 6 nullable properties warnings for EF Core models
I'm using EF Core, and I have this model:我正在使用 EF Core,我有这个 model:
public class Article
{
public int Id { get; set; }
public User Author { get; set; } // <- CS8618
public int AuthorId { get; set; }
}
As pointed out in the above code comment, I get an annoying nullable warning.正如上面的代码注释中所指出的,我收到了一个恼人的可为空的警告。 I'm using the Fluent API to annotate my model, and obviously Author is not nullable as AuthorId is not nullable.
我正在使用 Fluent API 来注释我的 model,显然作者不可为空,因为 AuthorId 不可为空。
I feel that using User? Author
我觉得使用
User? Author
User? Author
instead to make the warning go away is incorrect, as the property is obviously not nullable, and it would be a lie, and also a hint to people using my model that it is nullable.相反,
User? Author
将警告 go 去掉是不正确的,因为该属性显然不可为空,这将是一个谎言,同时也向使用我的 model 的人暗示它可以为空。
What is the proper way to deal with this situation?处理这种情况的正确方法是什么? Suppress the warning?
抑制警告?
You'd find a lot of different answers on this question你会在这个问题上找到很多不同的答案
But in this particular case, I would ask myself the question: who generates instances of this class?但在这种特殊情况下,我会问自己一个问题:谁生成了这个类的实例?
If you are 100% relying on EF Core to generate instances of this class for you by querying the database, and you are 100% sure that you ALWAYS use the proper Include()
, I would safely set the Author
field to null!
如果您 100% 依赖 EF Core 通过查询数据库为您生成此类的实例,并且您 100% 确定您始终使用正确的
Include()
,我会安全地将Author
字段设置为null!
, which means it assigns the default value null
on construction and automatically suppresses the nullability error. ,这意味着它在构造时分配默认值
null
并自动抑制可空性错误。
public class Article
{
public int Id { get; set; }
public User Author { get; set; } = null!;
public int AuthorId { get; set; }
}
You could also use the above solution with a private constructor to protect that no-one else but EF Core will generate instances of this class.您还可以将上述解决方案与私有构造函数一起使用,以保护除 EF Core 之外的其他人将生成此类的实例。
public class Article
{
private Article()
{
Author = null!;
}
public int Id { get; set; }
public User Author { get; set; }
public int AuthorId { get; set; }
}
If you generate instances of this class in any other parts of your code, you would need a constructor to ensure that Author
is never null after construction.如果您在代码的任何其他部分生成此类的实例,则需要一个构造函数来确保
Author
在构造后永远不会为空。
public class Article
{
public Article()
{
Author = new User();
}
public int Id { get; set; }
public User Author { get; set; }
public int AuthorId { get; set; }
}
If you do not allow a default User
object, it would make sense to expect a null
value for Author
but mark it Required
to ensure that it will never be null
when written to database.如果您不允许默认的
User
对象,则期望Author
的null
是有意义的,但将其标记为Required
以确保在写入数据库时它永远不会为null
。
public class Article
{
public int Id { get; set; }
[Required]
public User? Author { get; set; }
public int AuthorId { get; set; }
}
只需将“#nullable disable”放在命名空间上方即可。
Since C#11 ( .NET 7.0 ) you can add a required
modifier.从C#11 ( .NET 7.0 ) 开始,您可以添加
required
的修饰符。
public class Article
{
public int Id { get; set; }
public required User Author { get; set; }
public int AuthorId { get; set; }
}
And with the required
modifier, you won't be able to use var article= new Article();
使用
required
修饰符,您将无法使用var article= new Article();
to create an object instance.创建一个 object 实例。 So it's great protection for the not nullable property
User
.因此,它对不可为空的属性
User
提供了很好的保护。
Now to create an object instance you must do something like:现在要创建一个 object 实例,您必须执行如下操作:
var article= new Article()
{
User = new();
};
Originally answer was posted on: What is best way to create .NET6 class with many non-nullable properties?最初的答案发布于: What is best way to create .NET6 class with many non-nullable properties?
More detailed info: https://learn.microsoft.com/en-us/do.net/csharp/language-reference/keywords/required更详细的信息: https://learn.microsoft.com/en-us/do.net/csharp/language-reference/keywords/required
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.