I have a problem with updating data base through entity frame work.
My XAML code:
<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:
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. Database (.mdf1) was create when I was creating entity Model. 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"); and the method 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. 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);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.