简体   繁体   English

'可为空的 object 必须有一个值。' 对于列表 LINQ c#

[英]'Nullable object must have a value.' for List in LINQ c#

In database I have next model tblWorkItem with next field:在数据库中,我有下一个 model tblWorkItem和下一个字段:

public List<xWorkItemItemFailReason> WorkItemItemFailReasons { get; set; }

When I am trying to get tblWorkItem data from database当我试图从数据库中获取tblWorkItem数据时

_db.tblWorkItem.Where(x=>x.WorkItemItemFailReasons != null)

I have next error我有下一个错误

System.InvalidOperationException: 'Nullable object must have a value.' System.InvalidOperationException:“可空 object 必须有一个值。”

How I can check if this field is not null?我如何检查此字段是否不是 null?

Problem in xWorkItemItemFailReason class. xWorkItemItemFailReason类中的问题。 It has some property of primitive type ( int , long , ...) that is not nullable, but has no value in database table.它具有一些不可为空的原始类型( intlong 、...)属性,但在数据库表中没有值。 Check them.检查它们。

In your modal class have some not nullable field so when fetching from database it comes with null values.在您的模态类中有一些不可为空的字段,因此当从数据库中获取时,它带有空值。 so make it nullable .所以让它可以为空。 public int? FieldName {Get;set;}

if its .net core 3.0 or earlier there is a known issue where如果它的 .net core 3.0 或更早版本存在一个已知问题

Simple query filter breaks simple projection https://github.com/dotnet/efcore/issues/13517简单的查询过滤器打破了简单的投影https://github.com/dotnet/efcore/issues/13517

You should also make sure there are no data integrity problems with your foreign key relationships.您还应该确保您的外键关系没有数据完整性问题。 A foreign key value in the parent table that doesn't exist in the child table will throw this exception too (eg, you set the foreign key to 0 instead of null if there's no child, and no child record in the database has a key value of 0).父表中的外键值在子表中不存在也会抛出此异常(例如,如果没有子表,则将外键设置为 0 而不是 null,并且数据库中的子记录没有键值为 0)。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM