Im新增ASP.NET MVC3。我卡住了这个错误,当我在这些视频这里做着同样的事情:该名称在当前上下文中不存在
(我看着堆其他类似的问题,但没有找到解决方案)
我的代码是:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace testbaza.Controllers
{
public class KorController : Controller
{
private EntitiesModel dbContext;
protected override void Initialize(System.Web.Routing.RequestContext requestContext)
{
base.Initialize(requestContext);
if (this.Session[ContextModule.Content_KEY] != null)
{
this.dbContext = this.Session[ContextModule.Content_KEY] as EntitiesModel;
}
else {
throw new Telerik.OpenAccess.Exceptions.NoSuchObjectException("Cannot find EntitiesModel", null);
}
}
,并即时得到这个埃罗r:名称'ContextModule'在当前上下文中不存在。
这是我进一步的代码之前,我做到了:
我加入这个项目\ Web.config中(同视频1):
<httpModules>
<add name="ContextModule" type="testbaza.ContextModule, testbaza"/>
</httpModules>
我添加了名为ASP.NET模块“ContextModule” 到\项目(同视频)
这是ContextModule.cs:
using System;
using System.Web;
namespace testbaza.Models
{
public class ContextModule : IHttpModule
{
internal const string CONTEXT_KEY = "datacontext";
public void Dispose()
{
}
public void Init(HttpApplication context)
{
context.PostRequestHandlerExecute += new EventHandler(context_PostRequestHandlerExecute);
context.PreRequestHandlerExecute += new EventHandler(context_PreRequestHandlerExecute);
}
private void context_PreRequestHandlerExecute(object sender, EventArgs e)
{
if (HttpContext.Current.Session != null)
{
HttpContext.Current.Session[CONTEXT_KEY] = new EntitiesModel();
}
}
private void context_PostRequestHandlerExecute(object sender, EventArgs e)
{
CommitTransactions();
DisposeContext();
ClearSession();
}
private void CommitTransactions()
{
if (HttpContext.Current.Session == null)
{
return;
}
EntitiesModel dbContext =
HttpContext.Current.Session[CONTEXT_KEY] as EntitiesModel;
if (dbContext != null)
{
dbContext.SaveChanges();
}
}
private void DisposeContext()
{
if (HttpContext.Current.Session == null)
{
return;
}
EntitiesModel dbContext =
HttpContext.Current.Session[CONTEXT_KEY] as EntitiesModel;
if (dbContext != null)
{
dbContext.Dispose();
}
}
private void ClearSession()
{
if (HttpContext.Current.Session == null)
{
HttpContext.Current.Session.Remove(CONTEXT_KEY);
}
}
}
}
任何人都可以帮我解决这个问题吗? 在此先感谢!
哇...这是我见过的最糟糕的理念,过于复杂,没有解决一个真正的问题,而且是危险的引导。 Do * NOT *将实体框架上下文放入会话中。这是坏坏坏。 –
@MystereMan:听起来他正在使用Telerik OpenAccess,而不是EF。 – SLaks
@SLaks - 错过了,但仍然..我无法想象这将是一个在OpenAccess下的好主意。 –