c# - SQL 和 ASP.NET 从物化 'System.Int64' 类型到 'System.Int32' 类型的指定转换无效

标签 c# sql asp.net .net entity-framework

我正在使用 ASP.NET Entity Framework ,我正在尝试创建一个 SqlQuery,但我收到此错误:

The specified cast from a materialized 'System.Int64' type to the 'System.Int32' type is not valid.

我的问题是我不知道哪个列给我带来了麻烦以及如何解决它。这是我的代码。我不认为我正在做的是我的类型转换,因为那些是字符串。

return View(db.Data.SqlQuery("SELECT ROW_NUMBER() OVER(ORDER BY slotOrder ASC) AS id, 
                              concat(a.dateSlot, ' - ', a.timeSlot) as dateSlot, 
                              concat(a.dateSlot, '-', a.timeSlot) as timeSlot, 
                              COUNT(*) as slotOrder 
                              FROM Appointments a 
                              LEFT OUTER JOIN dateTimeSlots b 
                              ON a.dateSlot = b.dateSlot AND a.timeSlot = b.timeSlot 
                              GROUP BY a.dateSlot, a.timeSlot, b.slotOrder 
                              ORDER BY b.slotOrder").ToList());

这是我的类(class):

public class dateTimeSlots
{
    public int id { get; set; }
    [DisplayName("Date")]
    public string dateSlot { get; set; }
    [DisplayName("Time")]
    public string timeSlot { get; set; }
    [DisplayName("Order")]
    public int slotOrder { get; set; }
}

最佳答案

您上面的查询返回一个 Int64 (long) 并且您将其存储为一个 Int32 (int ).找出它是哪个数字字段并调整模型。

我的猜测是 idlong,因为它是 ROW_NUMBER()

关于c# - SQL 和 ASP.NET 从物化 'System.Int64' 类型到 'System.Int32' 类型的指定转换无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46060625/

相关文章:

c# - 在这种情况下我应该使用接口(interface)还是抽象类?

sql - 是否有一个函数可以将sql中的下一行列值交换到当前行列值

c# - 为什么这个 LINQ 连接查询有效,而另一个无效?

c# - 如何在 ASP.NET MVC (Orchard CMS) 中禁用防伪造?

c# - Visual Studio 2013 中的 "New Project"对话框中缺少项目模板

c# - 如何匹配一个字符串,但前提是同一字符串尚未匹配或不匹配破折号?

c# - 如何在 Orchard CMS 模块中连接到 Global.asax 提供的标准事件?

MySQL - 如何获取行数

c# - 我应该学习 SharePoint 2010 还是 SharePoint 2007?

c# - 如何复制表条目及其依赖表行mysql