C# SqLite EntityFramework表更新操作落后一步
如标题所示,当我更新SQLite db中任何表中的记录时,更新“落后一步” 我使用的是datagrid,在C# SqLite EntityFramework表更新操作落后一步,c#,wpf,sqlite,datagrid,C#,Wpf,Sqlite,Datagrid,如标题所示,当我更新SQLite db中任何表中的记录时,更新“落后一步” 我使用的是datagrid,在CellEditEnding事件中,我调用以下方法: private void CellEdited(object sender, DataGridCellEditEndingEventArgs e) { Marcatore edited = (Marcatore)e.Row.Item; MainWindow._DbContext.Entry(edited).State = Syst
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>
我要编辑的表:
[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
之外,还有什么办法解决这个问题吗
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; }
}