在我的“@foreach(模型中的var项目)”视图中,我似乎无法避免“EntityCommandExecutionException未被用户代码处理”的错误。所以我从零开始创建了一个新项目 - > ASP.NET MVC 3 Web应用程序。 在此之后,我试图通过利用ADO.NET和LINQ来访问我的数据库,所以我添加了“Case.cs”的模型类
Case.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”的控制器
CaseController.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);
}
}
}
最后,我添加了名为“Index.cshtml”的CaseController.cs的索引视图
Index.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项目{“在Case的索引视图中。此外,我正在尝试使用Code First,所以我还没有创建表,但我已经点击了Build Solution。