1

我在通过实体框架更新数据库时遇到问题。

我的 XAML 代码:

<DockPanel Grid.Row="1"
           Margin="0,5,0,0"
           DataContext="{StaticResource directoryViewSource}">
  <DataGrid ItemsSource="{Binding}"
            AutoGenerateColumns="False"
            RowEditEnding="DataGrid_RowEditEnding">
    <DataGrid.Columns>
      <DataGridTextColumn Header="Id"
                          Binding="{Binding Path=id}"
                          Width="50"
                          CanUserResize="True"
                          IsReadOnly="True" />
      <DataGridTextColumn Header="Name"
                          Binding="{Binding Path=name, Mode=TwoWay}"
                          Width="*" />
      <DataGridTextColumn Header="Sername"
                          Binding="{Binding Path=sername, Mode=TwoWay}"
                          Width="*" />
      <DataGridTextColumn Header="Address"
                          Binding="{Binding Path=address, Mode=TwoWay}"
                          Width="*" />
      <DataGridTextColumn Header="Email"
                          Binding="{Binding Path=email, Mode=TwoWay}"
                          Width="*" />
      <DataGridTextColumn Header="Modified_by"
                          Binding="{Binding Path=users.user_name}"
                          Width="*"
                          IsReadOnly="True" />
    </DataGrid.Columns>
  </DataGrid>
</DockPanel>

和 c# 代码:

        WpfApplication1.db_test_directoryEntities1 db_test_directoryEntities1;

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

            this.db_test_directoryEntities1 = new WpfApplication1.db_test_directoryEntities1();
            System.Windows.Data.CollectionViewSource directoryViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("directoryViewSource")));
            System.Data.Objects.ObjectQuery<WpfApplication1.directory> directoryQuery = this.db_test_directoryEntities1.directory;
            directoryViewSource.Source = directoryQuery.Execute(System.Data.Objects.MergeOption.AppendOnly);
        }

        private void DataGrid_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e)
        {
            this.db_test_directoryEntities1.SaveChanges();
        }

所以.. Datacontext 更新但数据库没有。数据库 (.mdf1) 是在我创建实体模型时创建的。你怎么看?为什么数据库不更新

如果我在 SaveChanges() 上放置一个断点,那么我可以看到设置如何在 ReportPropertyChanged("name"); 和方法 OnnameChanged(); 不使用

public global::System.String name
        {
            get
            {
                return _name;
            }
            set
            {
                OnnameChanging(value);
                ReportPropertyChanging("name");
                _name = StructuralObject.SetValidValue(value, true);
                ReportPropertyChanged("name");
                OnnameChanged();
            }
        }
4

1 回答 1

1

RowEdidEnding 在提交修改的行之前被触发。也许您在将更改提交到绑定对象之前进行保存。

一个(不是很干净的)解决方案可能是:

private void DataGrid_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e)
{
      Dispatcher.BeginInvoke(
         new Action(()=>this.db_test_directoryEntities1.SaveChanges()), 
         System.Windows.Threading.DispatcherPriority.Background);
}
于 2013-10-04T15:29:36.543 回答