2013-10-23 69 views
0

我有EF5的问题。我正在使用MVC 4.5实体框架5与存储库检索旧数据

我试图使每个请求“模式”1上下文。

我没有使用“工作单元”模式,也没有使用测试或DI。

我正在使用通用存储库模式与数据库进行交互。每个存储库使用单身“DataContextManager”所使用的相同上下文。

在全局asax中的每个请求中,我刷新了上下文,但是发生了一些错误:ie:如果我在DB中更改了数据,我有一个分页列表并移动页面,因此它无法正确刷新。 这不是一个HTML缓存问题,我测试了它。

我知道是EF上下文的问题,因为我有“这样的事情”:

private static Context C; //for the singleton. And in global.asax 

public Application_BeginRequest() 
{  
    DataContextManager.RefreshNew(); 
} 

protected void Application_EndRequest(object sender, EventArgs e) 
{ 
    Domain.DataContextManager.Dispose(); 
} 

并在第一时间名单的工作,并在第二页我得到一个错误说, 上下文是处置。

我读了一些在静态变量中使用上下文的东西,但我不知道发生了什么。我想使用这样简单的东西,因为要实现UnitOfWork模式,我需要更改很多代码。

这里是我的课的一点snnipet:

public class DataContextManager 
    { 
     private static Entities _Context; 

     private const string ConnectionString = "connString"; 

     public static Entities Context 
     { 
      get 
      { 
       if (DataContextManager._Context == null) 
        DataContextManager._Context = new Entities(ConfigurationManager.ConnectionStrings[ConnectionString].ConnectionString); 

       return DataContextManager._Context; 
      } 
     } 

     //This method is not necessary but made it for testing 
     public static void RefreshNew() 
     {     
      DataContextManager._Context = new Entities(ConfigurationManager.ConnectionStrings[ConnectionString].ConnectionString); 
     } 

     public static void Dispose() 
     { 
      if (DataContextManager._Context != null) 
      { 
       DataContextManager._Context.Dispose(); 
       DataContextManager._Context = null; 
      } 
     } 
    } 

和存储库使用DataContextManager这样的:提前

public class BaseRepository<TEntity> where TEntity : class 
    { 
     internal Entities context; 
     internal DbSet<TEntity> dbSet; 

     public BaseRepository() 
      : this(DataContextManager.Context) 
     { 
     } 

     public BaseRepository(Entities context) 
     { 
      this.context = context; 
      this.dbSet = context.Set<TEntity>(); 
     } 

谢谢!

Pablo。

回答

0

我不知道你真的需要经理级,因为一切似乎只是访问DbContext属性。

这就是说,你通常应该避免在静态类中使用DbContext;我没有任何规范的来源来支持它,但我个人的经验是,它在某些时候会导致比静态类提供的任何好处更多的问题。所以,我会更新管理器,像这样:

public class DataContextManager 
{ 
    private readonly string connectionToUse = string.Empty; 

    private Entities _context; 

    public Entities Context 
    { 
     get 
     { 
      if (_context == null) 
      { 
       _context = new Entities(WebConfigurationManager.ConnectionStrings[connectionToUse].ConnectionString); 
      } 

      return _context; 
     } 
    } 

    public DataContextManager() 
    { 
     connectionToUse = "connString"; 
    } 

    public DataContextManager(string key) 
    { 
     connectionToUse = key; 
    } 


    #region IDisposable Members 

    public void Dispose() 
    { 
     this.Dispose(true); 

     GC.SuppressFinalize(this); 
    } 

    protected virtual void Dispose(bool disposeAll) 
    { 
     if (disposeAll) 
     { 
      _context.Dispose(); 
     } 
     _context = null; 
    } 

    #endregion 
} 

然后你只是一个受保护字段添加到您的每个控制器和实例化管理器在控制器的构造函数:

protected DataContextManager Manager = null; 

public HomeController() 
{ 
    Manager = new DataContextManager(); 

    // or 
    // 
    //__manager = new DataContextManager("connection-To-Use"); 
} 

如果您的所有控制器使用相同的DbContext类,则可以创建BaseController类,该类继承System.Web.Mvc.Controller并将管理器移入其中,这样可以节省一些重复。