WPF和Entity Framework 4.0:检测数据库更改并更新DataGrid [英] WPF and Entity Framework 4.0: Detecting changes of Database and update DataGrid

查看:131
本文介绍了WPF和Entity Framework 4.0:检测数据库更改并更新DataGrid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我第一次在这里问一个问题.希望我做对了.

我有一个使用EF连接到数据库的应用程序(WPF).我需要检测由外部应用程序执行的数据库更改,然后在我的应用程序中重新加载数据以保持最新. WPF应用程序使用Datagrid,该数据网格已使用EF绑定到数据库.

实际上我有两个问题:

1)
当我手动刷新数据时,网格中只会更新已更改的行.不会显示新行,也不会删除已删除的行.对我来说,绑定似乎有错误,但我看不到它.也许有人可以给我提示.

2)
如何自动检测数据库的更改?
我知道所有查询一经执行便是最新的.但我的尝试不是轮询数据库.相反,我想要一个事件.某事像消费一个事件ObjectContext.DbChanged一样,但是没有这样的事件(或者我没有找到它?).是否有任何简便"的方法可以使用实体框架检测数据库的更改?或者至少,我真的必须轮询数据库吗?

当然,我问谷歌,但也许我是用错误的方式问这个问题,所以答案并不令我满意.

任何提示都值得赞赏.

该数据库具有一个表Locations,其中包含行ID和TrackNumber.

这是我的XAML代码

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>




这是C#代码




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; // Locations is the Tabelname
}


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();
}

推荐答案

hi
我认为,如果您将实体中的数据填充到ObservableCollection类型的列表并将datagrid的itemsource设置为ObservableCollection list,您的问题将得到解决.
hi
I think if you fill data from entity to list of type ObservableCollection and set the itemsource of datagrid to ObservableCollection list,your problem will be solved.


这篇关于WPF和Entity Framework 4.0:检测数据库更改并更新DataGrid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆