简体   繁体   English

EntityFramework不会更新数据库

[英]EntityFramework doesn't update database

I have a problem with updating data base through entity frame work. 我在通过实体框架工作更新数据库时遇到问题。

My XAML code: 我的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>

And c# code: 和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();
        }

So.. Datacontext updates but the database is not. 因此,.Datacontext更新,但数据库未更新。 Database (.mdf1) was create when I was creating entity Model. 创建实体模型时已创建数据库(.mdf1)。 What do you think? 你怎么看? Why database doesn't update 为什么数据库不更新

if I put a breakpoint on SaveChanges() then I can see how setting stopped on ReportPropertyChanged("name"); 如果我在SaveChanges()上设置一个断点,则可以看到如何在ReportPropertyChanged(“ name”)上停止设置; and the method OnnameChanged(); 和方法OnnameChanged(); doesn't use 不使用

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

RowEdidEnding is fired just before committing the modified row. 在提交修改后的行之前,将触发RowEdidEnding。 Maybe you are saving before the changes are committed to the bound object. 可能是在保存更改之前将更改提交到绑定对象。

A (not very clean) solution might be: 一个(不是很干净的)解决方案可能是:

private void DataGrid_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e)
{
      Dispatcher.BeginInvoke(
         new Action(()=>this.db_test_directoryEntities1.SaveChanges()), 
         System.Windows.Threading.DispatcherPriority.Background);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM