Entity Framework Core 版本是 8.0。这是我的模型。
public class Item
{
[Key] public long ItemId { get; set; }
public string KeyNumYear { get; set; }
...
}
在数据库中,KeyNumYear
是可空的int列。
我们有一行字段值为空KeyNumYear
。使用 EfCore 查询此行时,出现以下错误。
System.Data.SqlTypes.SqlNullValueException: “数据为 Null。无法对 Null 值调用此方法或属性。”
但如果我将其改为public string? KeyNumYear { get; set; }
,它就能成功运行。
我很困惑,因为我认为string
在 C# 中已经是可空的。为什么我必须将其标记为string?
在 Entity Framework Core 中?