c# - 不能将 null 值分配给类型为 System.Int32 的成员,该类型是不可为 null 的值类型

标签 c# asp.net sql linq sql-server-2008

运行这段代码。在本地开发服务器上运行良好,但在实时服务器上运行不正常。

try
{
    var qq =
    db.tblConstructReleaseDownloads
    .Where(c => c.UserID.HasValue && c.UserID.Value == userID)
    .OrderBy(c => c.DateStarted)
    .ThenBy(c => c.ReleaseID);

    var query = db.GetCommand(qq).CommandText;
    HttpContext.Current.Response.Write(query + "<br><br>");

    foreach (var d in qq)
    {
        HttpContext.Current.Response.Write("ID:" + d.ID + "<br>");
    }

这会引发错误:

The null value cannot be assigned to a member with type System.Int32 which is a non-nullable value type.

它打印出来的命令文本是:

SELECT     ID, ReleaseID, IP, DateCompleted, BytesServed, UserID, DateStarted, BytesPerSecond, TrackerVisitID
FROM         tblConstructReleaseDownloads AS t0
WHERE     (UserID IS NOT NULL) AND (UserID = @p0)
ORDER BY DateStarted, ReleaseID

我在实时数据库上运行这个查询,它工作正常,没有错误。

有人知道是什么原因造成的吗?

最佳答案

查询返回的其中一列在实时服务器的数据库中包含一个 NULL 值,但在开发服务器上不存在。
因为域模型中的相应属性定义为 int 而不是 int?,所以您应该将该列标记为不可为空

关于c# - 不能将 null 值分配给类型为 System.Int32 的成员,该类型是不可为 null 的值类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16148335/

相关文章:

c# - 检索系统日期

c# - 强制 .NET、多线程 volatile 优化错误

c# - Entity Framework - 有条件地包括相关实体

asp.net - 单击事件在 CSS 中不起作用

sql - MIN/MAX 与 ORDER BY 和 LIMIT

c# - 如何使用非托管导出将函数指针从 C++ 发送到 C# dll 以用作回调

c# - 使用 DbContext 访问连接属性

asp.net - 经典 asp "An error occurred when verifying security for the message."iis7 传输级安全

sql - 当时间戳开始时间大于时间戳结束时间时查询失败

sql - 真的需要关系表吗?