0

如标题所示,当我在我的 SQLite 数据库中的任何表中更新记录时,更新是“落后一步”。

我正在使用数据网格,并且在CellEditEnding事件中我调用以下方法:

private void CellEdited(object sender, DataGridCellEditEndingEventArgs e)
{
  Marcatore edited = (Marcatore)e.Row.Item;
  MainWindow._DbContext.Entry(edited).State = System.Data.Entity.EntityState.Modified;
  MainWindow._DbContext.SaveChanges();
}

DataGrid 加载如下:

private void Windows_Loaded(object sender, System.Windows.RoutedEventArgs e)
{
    dataGrid.ItemsSource = MainWindow._DbContext.Marcatore.ToList();
}

xaml 看起来像这样:

<DataGrid x:Name="dataGrid" AutoGenerateColumns="False" EnableRowVirtualization="True" ItemsSource="{Binding}" RowDetailsVisibilityMode="VisibleWhenSelected" CellEditEnding="CellEdited">
    <DataGrid.Columns>
        <DataGridTextColumn x:Name="idColumn" Binding="{Binding ID}" Visibility="Hidden"/>
        <DataGridTextColumn x:Name="modelColumn" Binding="{Binding Model}" Header="Marker Model" Width="Auto"/>
        <DataGridTextColumn x:Name="comInterfaceColumn" Binding="{Binding ComInterface}" Header="Communication Interface" Width="Auto"/>
    </DataGrid.Columns>
</DataGrid>

假设我编辑了一个从 1 到 2 的单元格值。如果我检查 DB,则引用的值仍然是 1。如果我将相同的单元格值从 2 编辑到 3 并检查 DB,则该值现在是 2。依此类推。

这是数据库上下文:

public class DatabaseContext : DbContext
{
    public DatabaseContext():
        base(new SQLiteConnection()
        {
          ConnectionString = new SQLiteConnectionStringBuilder() { DataSource = "MarkingGateway.db", ForeignKeys = true }.ConnectionString
        }, true)
{
  Database.ExecuteSqlCommand("PRAGMA foreign_keys = ON");
}

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

public DbSet<Marcatore> Marcatore { get; set; }
}

SQLite配置:

public class SQLiteConfiguration : DbConfiguration
{
    public SQLiteConfiguration()
    {
        SetProviderFactory("System.Data.SQLite", SQLiteFactory.Instance);
        SetProviderFactory("System.Data.SQLite.EF6", SQLiteProviderFactory.Instance);
        SetProviderServices("System.Data.SQLite", (DbProviderServices)SQLiteProviderFactory.Instance.GetService(typeof(DbProviderServices)));
    }
}

我要编辑的表:

[Table(Name = "Marcatore")]
public class Marcatore    
{
    [Column(Name = "ID", IsDbGenerated = true, IsPrimaryKey = true, DbType = "INTEGER")]
    [Key]
    public int ID { get; set; }

    [Column(Name = "Model", DbType = "TEXT")]
    public string Model { get; set; }

    [Column(Name = "ComInterface", DbType = "TEXT")]
    public string ComInterface { get; set; }
}

Savechanges除了做两次之外,有什么想法可以解决这个问题吗?

4

0 回答 0