【问题标题】:SQL and ASP.NET The specified cast from a materialized 'System.Int64' type to the 'System.Int32' type is not validSQL 和 ASP.NET 从具体化的“System.Int64”类型到“System.Int32”类型的指定转换无效
【发布时间】:2018-02-14 01:59:55
【问题描述】:

我正在使用 ASP.NET 实体框架,我正在尝试创建一个 SqlQuery 但我收到此错误:

从具体化的 'System.Int64' 类型到 “System.Int32”类型无效。

我的问题是我不知道哪一栏给我带来了麻烦以及如何解决它。这是我的代码。我不认为这是我在做的演员,因为那些是字符串。

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());

这是我的课:

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; }
}

【问题讨论】:

  • 您将其存储到的模型类是什么?请也添加它。 idslotOrder 都是长整数而不是整数。应该是id
  • 在我的问题中添加了类。

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


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    ROW_NUMBER() 返回 bigint (MSDN),在 C# 中转换为 Int64long

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多