0

Im新增ASP.NET MVC3。我卡住了這個錯誤,當我在這些視頻這裏做着同樣的事情:該名稱在當前上下文中不存在

http://tv.telerik.com/watch/orm/building-a-mvc-3-application-database-first-with-openaccess-creating-model?seriesID=1529

http://tv.telerik.com/watch/orm/building-a-mvc-3-application-database-first-with-openaccess-creating-controllers

(我看着堆其他類似的問題,但沒有找到解決方案)

我的代碼是:

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); 
      } 
     } 
    } 
} 

任何人都可以幫我解決這個問題嗎? 在此先感謝!

+2

哇...這是我見過的最糟糕的理念,過於複雜,沒有解決一個真正的問題,而且是危險的引導。 Do * NOT *將實體框架上下文放入會話中。這是壞壞壞。 –

+1

@MystereMan:聽起來他正在使用Telerik OpenAccess,而不是EF。 – SLaks

+0

@SLaks - 錯過了,但仍然..我無法想象這將是一個在OpenAccess下的好主意。 –

回答

3

在你的控制器中添加以下內容:

using testbaza.Models; 

,你應該確定。

希望這有助於

2

那類在testbaza.Models命名空間中定義,你的控制器不在。
你需要使用using語句導入該命名空間。

+0

謝謝你的好回答! – user1598696

相關問題