Hi all,
First time I'm asking a question here. Hope I did it correct.
I have an appliaction (WPF) which uses EF to connect to a database. My need is to detect changes of the database which are performed by external apps and then to reload the data in my application to be up to date. The WPF app uses a Datagrid which is binded to the database using EF.
In fact I Have two questions:
1)
When I'm manualy refreshing the data, only changed rows are updated in the grid. Neither new rows are schown nor deleted rows are removed. For me, it looks like I have an error in my binding, but I don't see it. Maybe someone can give me a hint.
2)
How to automatically detect changes of the database?
I know that all queries are up to date as soon as they are executed. but my attempt is not to poll the database. Instead I want an event. Sth. like consuming an event ObjectContext.DbChanged, but there is no such event (or I did not find it?). Is there any "easy" way to detect changes of the database with entity framework? Or at least, do I realy have to poll the database?
Of course, I asked google, but maybe I'm asking the question in the wrong way, so the answers don't satisfy me.
Any kind of hints are appreciated.
The database has a table Locations with the rows ID and TrackNumber.
Here is my XAML code
<Window x:Class="MultiClientDb.Views.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:me="clr-namespace:MultiClientDb.ViewModels" Title="Window1"
Height="401" Width="691" mc:Ignorable="d"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:my="clr-namespace:EFDataAccess;assembly=EFDataAccess">
<Window.Resources>
<CollectionViewSource x:Key="locationViewSource"
d:DesignSource="{d:DesignInstance my:Location,
CreateList=True}" />
</Window.Resources>
<Grid DataContext="{StaticResource locationViewSource}">
<Button Content="Refresh" Height="23" HorizontalAlignment="Left"
Margin="568,152,0,0" Name="btRefresh"
VerticalAlignment="Top" Width="75"
Click="btRefresh_Click" />
<DataGrid AutoGenerateColumns="False" EnableRowVirtualization="True"
Height="200" HorizontalAlignment="Left"
ItemsSource="{Binding Source={StaticResource locationViewSource}}"
Name="locationDataGrid"
RowDetailsVisibilityMode="VisibleWhenSelected"
VerticalAlignment="Top" Width="400">
<DataGrid.Columns>
<DataGridTextColumn x:Name="trackNumberColumn"
Binding="{Binding Path=TrackNumber}"
Header="Track Number"
Width="SizeToHeader" />
<DataGridTextColumn x:Name="iDColumn"
Binding="{Binding Path=ID}"
Header="ID"
Width="SizeToHeader" />
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
And here's the C# code
private MultiClientModelContainer m_dataContext = new MultiClientModelContainer();
private void Window_Loaded(object sender, RoutedEventArgs e)
{
System.Windows.Data.CollectionViewSource locationViewSource =
((System.Windows.Data.CollectionViewSource)
(this.FindResource("locationViewSource")));
locationViewSource.Source = m_dataContext.Locations;
}
private void btRefresh_Click(object sender, RoutedEventArgs e)
{
System.Windows.Data.CollectionViewSource locationViewSource =
((System.Windows.Data.CollectionViewSource)
(this.FindResource("locationViewSource")));
m_dataContext.Refresh(
System.Data.Objects.RefreshMode.StoreWins,
m_dataContext.Locations);
BindingOperations.GetBindingExpression(
locationDataGrid,
DataGrid.ItemsSourceProperty).UpdateTarget();
}