如何加载数据库更改?

时间:2012-11-16 14:10:33

标签: c# .net entity-framework .net-4.0 entity-framework-5

  • 我正在使用Entity Framework 5和SQL Server 2008 R2。
  • 我还实现了存储库模式。
  • 这是一个桌面应用程序

我的系统运行良好,除了一个“小”细节。 当两个或多个用户正在使用该应用程序且一个用户更改数据时,如果他没有离开并重新进入该应用程序,则另一个用户不会看到该数据。 我在这里得到了拉迪斯拉夫的答案:Load changes made in another DbContext

它有效。唯一的问题是,对于每个集合,我想确保它已更新,我必须对其进行硬编码。

即使它有效,我也不禁认为必须有更好的方法。 我不认为Entity Framework不知道数据库何时被更改并且需要重新检索数据。

那么,我做错了什么?

这就是我到目前为止所做的事情:

public class MyAppContext : DbContext 
{
    //DbSet<T> declarations
}

public class Context : IDisposable
{
    private readonly MyAppContext ctx;
    private Dictionary<Type, IRepository> repositories;

    private static Context instance;

    public static Context Instance
    {
        get
        {
            if (instance == null)
                instance = new Context();

            return instance;
        }
    }
    ...
}

// and then each repository have access to the context
public abstract class Repository<T> : IRepository
    where T : class
{
    protected readonly MyAppContext ctx;
    ...
}

如您所见,我使用“单例”模式来访问上下文并避免双重上下文异常。

有什么想法吗?

0 个答案:

没有答案