在实体框架中执行Sql查询

时间:2019-03-05 08:22:45

标签: asp.net-mvc entity-framework

我有一个jquery数据表,我想在其中显示从sql查询生成的摘要报告,但是我无法做到这一点,它在查询中显示错误。摘要报告包含所有月份的名称和每月的条目数。

错误:

  

System.Data.Entity.Core.EntityCommandExecutionException:'数据读取器与指定的'DHIFeedbackModel.FeedBack'不兼容。类型“ FeedbackUserName”的成员在数据读取器中没有具有相同名称的对应列。”

public ActionResult LoadData()
{
    using (DHIFeedbackEntities2 Ms = new DHIFeedbackEntities2())
    {

        //var summary = Ms.FeedBacks.SqlQuery("select * from [DHIFeedback].[dbo].[FeedBack]").ToList<FeedBack>();
        var summary = Ms.FeedBacks.SqlQuery(
            @"SELECT * 
            FROM 
                (
                SELECT 
                    YEAR([FeedBackDate])[Year],DATENAME(MONTH, [FeedBackDate])[Month],
                    COUNT(1)[FeedbackID] 
                FROM 
                    [DHIFeedback].[dbo].[FeedBack] 
                GROUP BY 
                    YEAR([FeedBackDate]
                ),
                DATENAME(MONTH, [FeedBackDate])) AS Monthlyupdate 
                PIVOT(SUM([FeedbackID]) FOR Month IN([January],[February],[March],[April],[May],[June],[July],[August],[September],[October],[November],[December])) AS MNamePivot 
            order by 1,2"
        ).FirstOrDefault<FeedBack>();
        return Json(new { data = summary }, JsonRequestBehavior.AllowGet);


    }
}

和javascript:

$(document).ready(function () {
            $('#Summary').DataTable({

                "processing": true,

                "ajax": {
                    "url": "/Summary/LoadData",
                    "type": "GET",
                    "datatype": "json",

                },
                "lengthMenu": [
                    [5, 10, 25, 50, 100, -1],
                    [5, 10, 25, 50, 100, "All"]
                ],
                "autoWidth": true,
                "responsive": true,
                "lengthChange": true,
                "ordering": true,
                "fnRowCallback": function (nRow, aData, iDisplayIndex) {
                    var oSettings = this.fnSettings();
                    $("td:first", nRow).html(oSettings._iDisplayStart + iDisplayIndex + 1);
                    return nRow;
                },
                "columns": [

                    { "data":"Year", "autoWidth": true },
                    { "data":"January", "autoWidth": true },
                    { "data":"February", "autoWidth": true },
                    { "data":"March", "autoWidth": true },
                    { "data":"April", "autoWidth": true },
                    { "data":"May", "autoWidth": true },
                    { "data":"June", "autoWidth": true },
                    { "data":"July", "autoWidth": true },
                    { "data":"August", "autoWidth": true },
                    { "data":"September", "autoWidth": true },
                    { "data":"October", "autoWidth": true },
                    { "data":"November", "autoWidth": true },
                    { "data":"December", "autoWidth": true }
                ]
            });
        });


 public partial class FeedBack
    {
        public int FeedbackID { get; set; }
        public string FeedbackUserName { get; set; }
        public string FeedBackUserEmailID { get; set; }
        public string FeedBackComment { get; set; }
        public string Designation { get; set; }
        public string Organization { get; set; }
        public string ContactNo { get; set; }
        public string City { get; set; }
        public Nullable<System.DateTime> FeedBackDate { get; set; }
        public Nullable<double> IsPublished { get; set; }
        public string Reply { get; set; }
        public Nullable<double> IsReplied { get; set; }
        public Nullable<System.DateTime> ReplyDate { get; set; }
        public string ReplyBy { get; set; }
        public string Sector { get; set; }
        public Nullable<int> Status { get; set; }

        public int Year { get; set; }
        public int January { get; set; }
        public int February { get; set; }
        public int March { get; set; }
        public int April { get; set; }
        public int May { get; set; }
        public int June { get; set; }
        public int July { get; set; }
        public int August { get; set; }
        public int September { get; set; }
        public int October { get; set; }
        public int November { get; set; }
        public int December { get; set; }
        public string Monthlyupdate { get; set; }
        public string Month{ get; set; }
        [NotMapped]
        public List<FeedBack> FeedBackCollection { get; set; }

1 个答案:

答案 0 :(得分:0)

该错误表示ORM期望包含FeedBacks的每个属性的结果集,其中包括名为FeedbackUserName的一个属性

因此,您需要编辑sql的select子句以返回所有预期的列。

或者您可以使用

Ms.Database.SqlQuery<SomeType>(
                   "your query here").ToList();

其中SomeType是:

public class SomeType {
    public int Year {get; set;}        
    //... and/or all the pivoted columns
}