c# - LINQ 查询返回错误 "The expected type was ' System.Int3 2' but the actual value was null."

标签 c# linq asp.net-core-2.0

所以这是查询:

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/

相关文章:

c# - 从包含 Entity Framework 的 dll 获取连接字符串

c# - 当 FCK 编辑器放置在更新面板中时如何从 FCK 编辑器获取编辑值

c# - 如何使用 Selenium Webdriver 获取基于目标行的表格单元格文本

c# - MVC 批量编辑 - Linq to Sql 列表保存

asp.net-mvc - asp-net core2新的web项目问题

c# - 如何计算看起来非常接近 Winforms 控件的控件的禁用文本颜色?

c# - 如何在 Azure 移动服务 TableController GET 模板方法上返回 BadRequest?

c# - 如何在 EF Core 中连接多个表中仅包含某些列的数据集?

c# - 在带有 .NET Core 的库 csproj 中使用 Selenium WebDriver

.net - 使用 ASP.Net Core 2 Web 应用程序使用 EF6 调用 Full 库