鉴于:
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
}
}
UnitOfWork
是一次性的,可以处理我的 DbContext。它还禁用构造函数中的延迟加载:
public UnitOfWork()
{
_dbContext.Configuration.LazyLoadingEnabled = false;
Repository = new GenericRepository<MyEntities>(_dbContext);
}
public void Dispose()
{
Repository.Dispose();
}
Find<EntityAddress>()
实施的结果是:
_dbContext.Set<EntityAddress>().Where(predicate)
其中 predicate 是类型的参数Expression<Func<EntityAddress, bool>>
为什么即使在禁用延迟加载后仍会出现处理异常?