【发布时间】:2021-11-22 16:04:37
【问题描述】:
使用 Net 6 和实体框架我有以下实体:
public class Address {
public Int32 Id { get; set; }
public String CountryCode { get; set; }
public String Locality { get; set; }
public Point Location { get; set; }
public virtual Country Country { get; set; }
public virtual ICollection<User> Users { get; set; } = new List<User>();
}
在项目定义中使用 <Nullable>enable</Nullable> 我收到警告:
Non-nullable property '...' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
在地址属性中:
CountryCode, Locality, Location and Country.
我看到了一些解决这个问题的方法:
public String? CountryCode { get; set; }
public String CountryCode { get; set; } = null!;
我也可以添加一个构造函数,但并非所有属性(例如导航属性)都可以在构造函数中。
最好的方法是什么?
【问题讨论】:
-
documentation 显示了一些可能性。但我同意,这不是最重要的。
标签: c# .net-core entity-framework-core .net-6.0