0

在我对“@foreach(模型中的变量项)”的看法中,我似乎无法避免“EntityCommandExecutionException 未被用户代码处理”的错误。所以我从头开始创建了一个新项目 -> ASP.NET MVC 3 Web Application。在此之后,我尝试使用 ADO.NET 和 LINQ 访问我的数据库,所以我添加了“Case.cs”的模型类

案例.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace CustomerService.Models
{
    public class Case
    {
        public int ID { get; set; }
        public string Creator { get; set; }
        public System.DateTime CreatedDate { get; set; }
        public string CaseLead { get; set; }
        public string CaseType { get; set; }
        public string CaseSubType1 { get; set; }
        public string CaseSubType2 { get; set; }
        public string CaseStatus { get; set; }
        public string CaseTitle { get; set; }
        public string CaseDescription { get; set; }
        public string CaseContact { get; set; }
        public string CustomerID { get; set; }
        public string LocationID { get; set; }
        public string SystemID { get; set; }
    }
}

然后我将 DbContext 添加为“CSdb.cs”

CSdb.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;

namespace CustomerService.Models
{
    public class CSdb : DbContext
    {
        public DbSet<Case> Cases { get; set; }
    }
}

然后我添加了一个名为“CaseController.cs”的控制器

案例控制器.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CustomerService.Models;

namespace CustomerService.Controllers
{
    public class CaseController : Controller
    {
        //
        // GET: /Case/

        CSdb db = new CSdb();

        public ActionResult Index()
        {
            var model = db.Cases;

            return View(model);
        }

    }
}

最后,我为 CaseController.cs 添加了名为“Index.cshtml”的索引视图

索引.cshtml

@model IEnumerable<CustomerService.Models.Case>

    @{
        ViewBag.Title = "Index";
    }

    <h2>Index</h2>

    <p>
        @Html.ActionLink("Create New", "Create")
    </p>
    <table>
        <tr>
            <th>
                Creator
            </th>
            <th>
                CreatedDate
            </th>
            <th>
                CaseLead
            </th>
            <th>
                CaseType
            </th>
            <th>
                CaseSubType1
            </th>
            <th>
                CaseSubType2
            </th>
            <th>
                CaseStatus
            </th>
            <th>
                CaseTitle
            </th>
            <th>
                CaseDescription
            </th>
            <th>
                CaseContact
            </th>
            <th>
                CustomerID
            </th>
            <th>
                LocationID
            </th>
            <th>
                SystemID
            </th>
            <th></th>
        </tr>

    @foreach (var item in Model) {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Creator)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.CreatedDate)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.CaseLead)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.CaseType)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.CaseSubType1)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.CaseSubType2)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.CaseStatus)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.CaseTitle)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.CaseDescription)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.CaseContact)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.CustomerID)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.LocationID)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.SystemID)
            </td>
            <td>
                @Html.ActionLink("Edit", "Edit", new { id=item.ID }) |
                @Html.ActionLink("Details", "Details", new { id=item.ID }) |
                @Html.ActionLink("Delete", "Delete", new { id=item.ID })
            </td>
        </tr>
    }

    </table>

现在,当我调试它并导航到该视图时,我收到错误“EntityCommandExecutionException 未被用户代码处理 - 执行命令定义时发生错误。有关详细信息,请参阅内部异常”,并且此消息指向“@foreach( var item in Model) {" 在我的案例索引视图中。另外,我正在尝试使用 Code First,所以我还没有创建表,但我已经点击了构建解决方案。


任何建议将不胜感激。谢谢

4

0 回答 0