我将一个字符串传回我的控制器,然后尝试在 entity framework 上做一些原始的 sql。

I am passing a string back to my controller then trying to do some raw sql on entity framework.

我正在使用 linq,但我 运行 进入了我需要快速编写的东西,所以我试图让原始 sql 工作。这是我在 mvc 中的第一个项目,所以我是新手,但这是我的控制器代码

public ActionResult Index(string Vendors)
    {
        if (Vendors != "")
        {


            string query = "SELECT V.VENDOR_NAME, SUM(POL.ORDER_QTY * POL.UNIT_PRICE) AS AMOUNT_SPENT, MONTH(PO.ORDER_DATE) AS MO, YEAR(PO.ORDER_DATE) AS YR, M.ITEM_TYPE AS ITEM_TYPE "
                            + "FROM PO_LINE POL"
                            + "JOIN PURCH_ORD PO ON PO.PO_NUM = POL.PO_NUM"
                            + "JOIN VENDOR V ON V.VENDOR_ID = PO.VENDOR_ID"
                            + "JOIN MATERIAL M ON M.ITEM_ID = POL.ITEM_ID"
                            + "WHERE V.VENDOR_NAME =" + Vendors
                            + "GROUP BY V.VENDOR_NAME, M.ITEM_TYPE, YEAR(PO.ORDER_DATE), MONTH(PO.ORDER_DATE)"
                            + "ORDER BY V.VENDOR_NAME";

            var data = db.Database.SqlQuery<Reports>(query);
            ViewBag.Vendors = new SelectList(db.Vendors, "VENDOR_NAME", "VENDOR_NAME");
            return View(query.ToList());
        }

这是我的模态代码

           using System;
           using System.Collections.Generic;
           using System.Linq;
           using System.Text;


           namespace GreenfieldGroup2.Models.Reports
      {
         public class Reports
     {
    public string VENDOR_NAME { get; set; }
    public decimal AMOUNT_SPENT { get; set; }
    public string ITEM_TYPE { get; set; }
    public DateTime ORDER_DATE { get; set; }
    public string ITEM_NAME { get; set; }
    public int STOCK_QTY { get; set; }
    public string WARD_NAME { get; set; }
    public string WARD_LOCATION { get; set; }
    public int ITEM_ID { get; set; }
    public DateTime DEL_DATE { get; set; }
}

}

传入字典的模型项是'System.Collections.Generic.List1[System.Char]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable1[GreenfieldGroup2.Models.Reports.Reports]'类型。

是我的错误,我不确定这是什么意思。下面是我的视图代码

         @model IEnumerable<GreenfieldGroup2.Models.Reports.Reports>
      @{
         ViewBag.Title = "Index";
      }

     <br />
       <head>

<style>
    body {
    padding-top: 100px
    }
</style>
  </head>

    <body>
   @using (Html.BeginForm("Index", "PurchasingReport", FormMethod.Get))
    {
       <table>
           <tr>
    <td align="left"><text>Select a Vendor:</text>

    </td>
    <td align="left">@Html.DropDownList("Vendors", "All Vendords")</td>

</tr>
<tr>
    <td align="Left">
        <input type="submit" value="submit" onsubmit="return Verify()"/>
    </td>
</tr>

</table>
}
<table border="1">
<tr>
   <th>

        @Html.DisplayNameFor(model => model.VENDOR_NAME)
       </th>

    <th>
        @Html.DisplayNameFor(model => model.AMOUNT_SPENT)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.ITEM_TYPE)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.ORDER_DATE)
    </th>


</tr>

@foreach (var item in Model)
{
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.VENDOR_NAME)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.AMOUNT_SPENT)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.ITEM_TYPE)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.ORDER_DATE)
        </td>

    </tr>
}
 </table>


 </body>

任何帮助将不胜感激这是针对学校项目的

您应该更改您的 return 声明

return View(query.ToList());

以下内容:

return View(data);

据我了解,您发布的代码如下

var data = db.Database.SqlQuery<Reports>(query);

您将对 Reports 对象序列的引用分配给 data。换句话说,data 的类型是 IEnumerable<GreenfieldGroup2.Models.Reports.Reports>,正如我可以从您的代码中推断的那样。

因此需要将其传递给视图而不是 sql 语句,它只是一个字符串。

如果您查看您的视图,它期望“@model IEnumerable”,并且您传递给视图的参数是您在数据库上执行的查询,而不是传递查询传递 list,它将完美地工作并且不会给你上面的错误。

谢谢。

这是我的最终解决方案,所以如果其他人遇到这个问题,它可以完美运行并且没有 sql 注入

        if (Vendors == null) { Vendors = ""; }
            if(Vendors == "") { Vendors = DBNull.Value.ToString(); }

            var @Vendor = new SqlParameter("@Vendor", Vendors);

            string query = "SELECT V.VENDOR_NAME, SUM(POL.ORDER_QTY * POL.UNIT_PRICE) AS AMOUNT_SPENT, MONTH(PO.ORDER_DATE) AS MO, YEAR(PO.ORDER_DATE) AS YR, M.ITEM_TYPE AS ITEM_TYPE "
                + "FROM PO_LINE POL "
                + "JOIN PURCH_ORD PO ON PO.PO_NUM = POL.PO_NUM "
                + "JOIN VENDOR V ON V.VENDOR_ID = PO.VENDOR_ID "
                + "JOIN MATERIAL M ON M.ITEM_ID = POL.ITEM_ID ";

            if (Vendors != "")
            {
                query += "WHERE V.VENDOR_NAME = @Vendor ";
            }

            query += "GROUP BY V.VENDOR_NAME, M.ITEM_TYPE, YEAR(PO.ORDER_DATE), MONTH(PO.ORDER_DATE) "
                + "ORDER BY V.VENDOR_NAME";


            if (Vendors != "")
            {
                var data = db.Database.SqlQuery<Reports>(query, @Vendor);
                return View(data.ToList());
            }
            else
            {
                var data = db.Database.SqlQuery<Reports>(query);
                return View(data.ToList());
            }