我在EF 4.1中编写了一个简单的应用程序,它将使用添加,删除,编辑和详细信息构成我的公共数据源(数据库的中央服务器).在我的Controller类中,我写道:
public class RegController : Controller
{
//
// GET: /Reg/
private string CmdStr = ConfigurationManager.ConnectionStrings["MyConn"].ConnectionString;
public ActionResult Index()
{
using (var db = new RegModelContext(CmdStr))
{
return View(db.Registrant);
}
}
}
Run Code Online (Sandbox Code Playgroud)
当我执行我的应用程序时,它在foreach语句的索引视图中给了我一个错误:
@model IEnumerable<Registration.Models.Register>
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<title>Index</title>
</head>
<body>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th></th>
<th>
UserName
</th>
<th>
Password
</th>
<th>
Email
</th>
<th>
Address
</th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.ActionLink("Edit", "Edit", new …Run Code Online (Sandbox Code Playgroud) 鉴于:
public ActionResult List()
{
using (var unitOfWork = new UnitOfWork())
{
var result = unitOfWork.Repository.Find<EntityAddress>(a => a.PostalCode == "80001");
//return View(result.ToList());//NO Exception raised with ToList()
return View(result);//EXCEPTION RAISED IN VIEW DURING ITERATION
}
}
Run Code Online (Sandbox Code Playgroud)
UnitOfWork是一次性的,处理我的DbContext.它还禁用构造函数中的延迟加载:
public UnitOfWork()
{
_dbContext.Configuration.LazyLoadingEnabled = false;
Repository = new GenericRepository<MyEntities>(_dbContext);
}
public void Dispose()
{
Repository.Dispose();
}
Run Code Online (Sandbox Code Playgroud)
而Find<EntityAddress>()实施工程以:
_dbContext.Set<EntityAddress>().Where(predicate) 其中谓词是类型的参数 Expression<Func<EntityAddress, bool>>
为什么即使在禁用延迟加载后也会出现处理异常?