【问题标题】:Refresh data in System.Data.Entity.DbContext刷新 System.Data.Entity.DbContext 中的数据
【发布时间】:2016-03-07 16:50:19
【问题描述】:

我们将数据存储在 SQLite 数据库中。数据显示在 DevExpress.XtraGrid.GridControl 中。为了获取相关数据,我们创建临时表并填充它。 数据上下文:

public class AccountsSQLiteDataContext : DbContext
{
    public AccountsSQLiteDataContext(IDbConnection connection)
        : base((SQLiteConnection)connection, false)
    {
        Database.SetInitializer<AccountsSQLiteDataContext>(null);
    }

    public DbSet<tmp_acc> TmpAccounts { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
    }
}

使用 DbContext:

AccountsSQLiteDataContext myContext = new AccountsSQLiteDataContext(myDbConnection);

public IQueryable AccountTable
{
    get
    {
        return myContext.TmpAccounts;
    }
}

DevExpress.Data.Linq.LinqServerModeSource myLinqSource =  = new DevExpress.Data.Linq.LinqServerModeSource();
myLinqSource.QueryableSource = AccountTable;
myGridControl.DataSource = myLinqSource;

在数据更改后我调用:myLinqSource.Reload();

并不是所有 SQLite 临时表中的数据变化都显示在网格中。添加和删​​除表行生效。但未显示单元格文本的更改。 过滤器函数对新值生效。见printscreen。在过滤器选项中是正确的值,在网格中是旧值。

如何在临时表更改后刷新存储在 DbContext 中的本地数据以显示正确的值?

【问题讨论】:

  • 你在使用 WinForms DataGridView 吗?
  • 我们使用 DevExpress UI 框架及其 DevExpress.XtraGrid.GridControl 和 DevExpress.XtraGrid.Views.Grid.GridView。

标签: c# entity-framework sqlite xtragrid


【解决方案1】:

【讨论】:

  • 我勾选了这个刷新选项,没有效果。
【解决方案2】:

我找到了两个解决方案。两者都取消了 EF 数据模型中项目的缓存:

  1. 在 SQL 表中使用自动增量字段,任何数据更改后都会重新创建表
  2. 使用 AsNoTracking 扩展方法从数据库加载实体:

    public IQueryable AccountTable { 得到 { 返回 myContext.TmpAccounts.AsNoTracking(); } }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多