C# 用户代码未处理EntityCommandExecutionException
我似乎无法避免在“@foreach(模型中的var项)”的视图中出现“EntityCommandExecutionException未经用户代码处理”的错误。所以我从头开始,创建了一个新的项目->ASP.NETMVC3Web应用程序。 在此之后,我试图利用ADO.NET和LINQ访问我的数据库,因此我添加了模型类“Case.cs” Case.csC# 用户代码未处理EntityCommandExecutionException,c#,asp.net-mvc-3,linq,ado.net,C#,Asp.net Mvc 3,Linq,Ado.net,我似乎无法避免在“@foreach(模型中的var项)”的视图中出现“EntityCommandExecutionException未经用户代码处理”的错误。所以我从头开始,创建了一个新的项目->ASP.NETMVC3Web应用程序。 在此之后,我试图利用ADO.NET和LINQ访问我的数据库,因此我添加了模型类“Case.cs” Case.cs using System; using System.Collections.Generic; using System.Linq; using Sy
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添加了名为“index.cshtml”的索引视图
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>
@model IEnumerable
@{
ViewBag.Title=“Index”;
}
指数
@ActionLink(“新建”、“创建”)
造物主
创建数据
案件线索
案例类型
案例1
案例2
案件状况
案例名称
案例描述
案件联系人
客户编号
位置ID
系统ID
@foreach(模型中的var项目){
@DisplayFor(modelItem=>item.Creator)
@DisplayFor(modelItem=>item.CreatedDate)
@DisplayFor(modelItem=>item.CaseLead)
@DisplayFor(modelItem=>item.CaseType)
@DisplayFor(modelItem=>item.CaseSubite1)
@DisplayFor(modelItem=>item.CaseSubite2)
@DisplayFor(modelItem=>item.CaseStatus)
@DisplayFor(modelItem=>item.CaseTitle)
@DisplayFor(modelItem=>item.CaseDescription)
@DisplayFor(modelItem=>item.CaseContact)
@DisplayFor(modelItem=>item.CustomerID)
@DisplayFor(modelItem=>item.LocationID)
@DisplayFor(modelItem=>item.SystemID)
@ActionLink(“编辑”,“编辑”,新的{id=item.id})|
@ActionLink(“详细信息”,“详细信息”,新的{id=item.id})|
@ActionLink(“删除”,“删除”,新的{id=item.id})
}
现在,当我调试并导航到此视图时,我得到了“EntityCommandExecutionException未经用户代码处理-执行命令定义时出错。有关详细信息,请参阅内部异常”的错误,此消息指向“@foreach(模型中的var项){”在我的案例索引视图中,我也尝试先使用代码,所以我还没有创建表,但我已经找到了构建解决方案。
任何建议都将不胜感激。谢谢我们将尝试将控制器的代码更改为
var model=db.Cases.ToList()
您将看到,如果您还没有任何数据库,将在那里引发异常。当您尝试从数据库获取数据时,会出现异常。请显示内部异常。因此,我添加了.ToList()我的控制器中确实出现了异常,但现在我应该在数据库中手动创建表吗?我认为“代码优先”会从我的模型自动创建此表?我的连接字符串指向本地数据库。“代码”“代码”首先,您使用的是什么版本的EF?请同时阅读:看看这是否会清除它。我是kin这是一个很好的例子,但是我不确定如何找到内部异常。但是EF是v4.0.30319,我没有写这篇文章,但是谢谢你提供链接。
@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>