【问题标题】:Incompatible Data Reader不兼容的数据读取器
【发布时间】:2013-07-31 21:30:52
【问题描述】:

将 MVC 4 与 EF 5x 结合使用,对他们来说非常新。

我的实体和上下文等是在 VS 2010 中从现有数据库自动创建的,但是当我尝试通过原始 sql 查询获取结果时,我收到了错误:

The data reader is incompatible with the specified 'PlatinumModel.Sales_Journal'. A member of the type, 'Line_No', does not have a corresponding column in the data reader with the same name.

与此错误相关的堆栈交换解决方案 (The data reader is incompatible . A member does not...) 表明该错误是由于缺少列或模型 edmx 需要更新(尽管由于数据库没有必要但我已经这样做了)没有被改变)

Sales_Journal.cs - Platinum.tt 文件的一部分

namespace PPoS_MVC_Site.Models
{
    using System;
    using System.Collections.Generic;

    public partial class Sales_Journal
    {
        public int Line_No { get; set; }
        public Nullable<System.DateTime> Date_Time { get; set; }
        public Nullable<byte> Workstation_No { get; set; }
        public Nullable<short> User_No { get; set; }
        public Nullable<int> Trans_No { get; set; }
        public Nullable<int> Invoice_No { get; set; }
        public string Account_No { get; set; }
        public string Product_Code { get; set; }
        public string Department_No { get; set; }
        public Nullable<float> Qty { get; set; }
        public Nullable<float> Pack_Size { get; set; }
        public Nullable<float> Ave_Cost { get; set; }
        public Nullable<float> Sales_Tax { get; set; }
        public Nullable<byte> Tax_Type { get; set; }
        public Nullable<float> Discount_Amt { get; set; }
        public Nullable<float> Dicount_Value { get; set; }
        public Nullable<float> Line_Total { get; set; }
        public Nullable<float> Function_Key { get; set; }
        public Nullable<byte> Location { get; set; }
        public Nullable<bool> Stock_Level { get; set; }
        public Nullable<int> Branch_No { get; set; }
        public Nullable<int> Cashup_No { get; set; }
        public string Extra { get; set; }
        public Nullable<decimal> Table_No { get; set; }
        public Nullable<decimal> Tab_No { get; set; }
        public Nullable<int> Covers { get; set; }
        public Nullable<int> User_Overide { get; set; }
        public string Room_No { get; set; }
        public string Res_No { get; set; }
        public string Instructions { get; set; }
        public string Order_no { get; set; }
        public Nullable<int> Points { get; set; }
        public string Time_Placed { get; set; }
        public Nullable<int> JobCard_No { get; set; }
        public Nullable<int> Quote_No { get; set; }
        public string Serial_No { get; set; }
    }
}

Sales_Journal SQL 创建代码

CREATE TABLE [dbo].[Sales_Journal](
    [Line_No] [int] IDENTITY(1,1) NOT NULL,
[Date_Time] [datetime] NULL,
[Workstation_No] [tinyint] NULL,
[User_No] [smallint] NULL,
[Trans_No] [int] NULL,
[Invoice_No] [int] NULL,
[Account_No] [nvarchar](16) NULL,
[Product_Code] [nvarchar](25) NULL,
[Department_No] [nvarchar](10) NULL,
[Qty] [real] NULL,
[Pack_Size] [real] NULL,
[Ave_Cost] [real] NULL,
[Sales_Tax] [real] NULL,
[Tax_Type] [tinyint] NULL,
[Discount_Amt] [real] NULL,
[Dicount_Value] [real] NULL,
[Line_Total] [real] NULL,
[Function_Key] [real] NULL,
[Location] [tinyint] NULL,
[Stock_Level] [bit] NULL,
[Branch_No] [int] NULL,
[Cashup_No] [int] NULL,
[Extra] [nvarchar](50) NULL,
[Table_No] [numeric](10, 1) NULL,
[Tab_No] [numeric](10, 1) NULL,
[Covers] [int] NULL,
[User_Overide] [int] NULL,
[Room_No] [char](10) NULL,
[Res_No] [char](10) NULL,
[Instructions] [nvarchar](250) NULL,
[Order_no] [varchar](40) NULL,
[Points] [int] NULL,
[Time_Placed] [varchar](5) NULL,
[JobCard_No] [int] NULL,
[Quote_No] [int] NULL,
[Serial_No] [varchar](25) NULL,
    CONSTRAINT [PK_Sales_Journal] PRIMARY KEY CLUSTERED 
    (
        [Line_No] ASC
    )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON, FILLFACTOR = 90) ON [PRIMARY]
) ON [PRIMARY]

控制器代码试图与之交互

public void calculateBasicDayData() {
    string sSqlCash = "SELECT * FROM Sales_Journal WHERE Function_Key = 9 AND CONVERT(DATETIME, FLOOR(CONVERT(FLOAT, Date_Time))) = '" + this.targetDate.ToString() + "'";
    string sSqlCashCount = "SELECT COUNT(Sales_Journal.Line_No) FROM Sales_Journal WHERE Function_Key = 9 AND CONVERT(DATETIME, FLOOR(CONVERT(FLOAT, Date_Time))) = '" + this.targetDate.ToString() + "'";
    var sJournalCash = platContext.Sales_Journal.SqlQuery(sSqlCash);

    this.NumberOfCashSales = platContext.Sales_Journal.SqlQuery(sSqlCashCount).Count();
    this.TotalCash = this.calculateDayTotalSales(sJournalCash);
}

private decimal calculateDayTotalSales(System.Data.Entity.Infrastructure.DbSqlQuery<Sales_Journal> sj)
{
    decimal cashtotal = 0;

    foreach (var jItem in sj.ToList())
    {
        cashtotal += decimal.Parse(jItem.Line_Total.ToString());
    }

    return cashtotal;
}

【问题讨论】:

  • 您在数据库中的Line_No 列是否可以为空?
  • 嗨 haim770,它是表的 IDENTITY 字段,所以没有。
  • 您是否尝试过从查询中删除 where 条件?
  • @polybios - 谢谢,这让我更进一步。用 COUNT 取出语句的 WHERE 并将其强制为常数进行测试,它工作正常。没有计数的 SELECT 与 WHERE 一起使用,因此问题似乎是在使用 WHERE 语句时尝试获取记录的 COUNT 时。

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


【解决方案1】:

由于Line_No 是主键,您应该可以将COUNT(Sales_Journal.Line_No) 替换为COUNT(*),因为无论如何都会优化查询。

【讨论】:

    【解决方案2】:

    您正在尝试将计数“投射”到 Sales_Journal 对象,这不起作用:

    platContext.Sales_Journal.SqlQuery(sSqlCashCount).Count()
    

    该 SQL 查询返回一个整数,然后尝试从中创建一个 Sales_Journal 对象。

    您是否尝试过改用 LINQ? 所以像:

    this.NumberOfCashSales = 
       platContext.Sales_Journal.Count(s => s.Function_Key == 9 && 
                                            (s.Date_Time != null && 
                                             s.Date_Time >= this.targetDate.Date &&
                                             s.Date_Time < this.targetDate.Date.AddDays(1)));
    

    这为您提供了强类型查询,而无需在代码中编写 SQL 查询(调试起来要困难得多!)。

    【讨论】:

    • 感谢撕裂。将代码更改为: this.NumberOfCashSales = sJournalCash.Count();这给了我一个记录。由于“s.Date_Time.Date”,逐字代码给出了一个错误,去掉了“.Date”,它返回了 0,但让我朝着正确的方向修复它。非常感谢您的帮助。
    • 是的,我想.Date 属性不起作用,因为它不能转换为有效的 SQL。将更新代码示例,以便检查 Date_Time 列是否介于开始日期和结束日期之间。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-18
    • 2018-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-28
    • 2020-11-13
    相关资源
    最近更新 更多