1

EF 5x で MVC 4 を使用し、非常に新しい。

私のエンティティやコンテキストなどは、既存のデータベースから 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;
}
4

2 に答える 2

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 クエリを記述する必要なく、厳密に型指定されたクエリが得られます (これはデバッグがはるかに困難です!)。

于 2013-07-31T14:51:18.083 に答える