实体框架 5,其中存储库检索旧数据

本文关键字:检索 数据 存储 框架 实体 | 更新日期: 2023-09-27 18:36:31

我对 EF5 有问题。我正在使用 MVC 4.5

我正在尝试使每个请求的 1 个上下文成为"模式"。

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

我正在使用通用存储库模式与数据库进行交互。每个存储库都使用由单例"DataContextManager"管理的相同上下文。

在全局 asax 中的每个请求中,我刷新了上下文,但发生了错误:即:我有一个分页列表,如果我手动更改数据库中的数据,它会按页面移动,它不会正确刷新。这不是 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 模式,我需要更改大量代码。

这是我的班级的小片段:

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

提前感谢!

巴勃罗。

实体框架 5,其中存储库检索旧数据

我不知道

你是否真的需要管理器类,因为一切似乎都只是访问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类,则可以创建一个继承System.Web.Mvc.ControllerBaseController类,并将管理器移动到其中,这样可以节省一些重复。