相关疑难解决方法(0)

由于已经处理了DbContext,因此无法完成操作

我在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)

c# asp.net-mvc-3

8
推荐指数
2
解决办法
2万
查看次数

"操作无法完成,因为DbContext已被处理"异常并且禁用了延迟加载

鉴于:

    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>>

为什么即使在禁用延迟加载后也会出现处理异常?

c# asp.net-mvc-3 entity-framework-5

3
推荐指数
1
解决办法
5349
查看次数

标签 统计

asp.net-mvc-3 ×2

c# ×2

entity-framework-5 ×1