繁体   English   中英

带有存储库的Entity Framework 5检索旧数据

[英]Entity Framework 5 with Repositories retrieving old data

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

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

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

我正在使用通用存储库模式与DB进行交互。 每个存储库使用由单例“ DataContextManager”维护的相同上下文。

在全局asax中的每个请求中,我都刷新了上下文,但是发生了一些错误:即:如果我手动更改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模式,我将需要更改很多代码。

这是我班上的小片段:

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

提前致谢!

巴勃罗。

我不知道您是否真的需要manager类,因为所有内容似乎都只能访问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类,并将管理器BaseController该类,从而节省了一些重复项。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM