所以这是查询:
from c in Contents
join cs in DepartmentSharings
on c.ContentId equals cs.ContentId
select c.PrivateCategoryId.Value
当我运行它时,我得到:
A database operation failed while processing the request. InvalidOperationException: An exception occurred while reading a database value for property 'DepartmentSharing.ContentId'. The expected type was 'System.Int32' but the actual value was null.
现在我检查了数据库中的 DepartmentSharings.ContentId
字段,它的 fk, int, not null
。在类中它是 public int ContentId { get;放; }
。 DepartmentSharing.ContentId
不能为 null
。此外,在 LinqPad 中,我可以看到它在失败前返回了 58 行。
为什么会出现此错误?
最佳答案
唯一会导致该错误的情况是 ContentId
是一个不可为空的 int 属性,但不知何故您的数据库表允许该列为 NULL 值。假设 EF 正在处理您的数据库,这不应该发生,但您可能在某个时候更改了某些内容并且没有正确迁移。
无论如何,您需要将属性类型更改为 int?
而不是 int
或更改表以使列 NOT NULL 并确保所有行都有一个非 NULL 值。
关于c# - LINQ 查询返回错误 "The expected type was ' System.Int3 2' but the actual value was null.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49066105/