从DB(EF,MVVM)刷新数据

时间:2016-01-19 13:44:22

标签: entity-framework mvvm

面对这样的情况。该程序正在运行。我从程序中更改表wrk_Device_Value NOT中的数据。接下来,运行用于填充集合的方法,该方法包含以下代码。查询数据库已完成(通过Profiler查看),但数据收集仍然是旧数据。

可能出现什么问题?

            ValuesList = new ObservableCollection<wrk_Device_Value>
            (_dataContext.wrk_Device_Value.Where(d => d.ID_Device == device.ID_Device).ToList());

主窗口:

    public partial class MainWindow : Window
{
    private readonly MainViewModel _mainViewModel = new MainViewModel();

    public MainWindow()
    {
        InitializeComponent();

        Messenger.Default.Register<string>("ShowMainWindow", p => { });

        Messenger.Default.Register<DEVICE>(this, "ShowDialogWindow", p =>
        {
            var dlg = new EditPropertyValuesWindow();
            dlg.Owner = this;
            dlg.DataContext = DataContext;
            dlg.ShowDialog();
        }
        );
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        DataContext = _mainViewModel;
    }
}

MainViewModel:

    public class MainViewModel : ViewModelBase, IDisposable, INotifyPropertyChanged
{
    private AVTEntities _dataContext;
    public ObservableCollection<spr_TypeDevice> TypesOfDevice { get; private set; }

    public MainViewModel()
    {
        this._dataContext = new AVTEntities();

        TypesOfDevice = new ObservableCollection<spr_TypeDevice>
            (_dataContext.spr_TypeDevice.OrderBy(a=>a.Name_TypeDevice)); 
        Save = new RelayCommand (SaveAllChanges);
    }


    private DEVICE _selectedDevice;
    public DEVICE SelectedDevice
    {
        get
        {
            return _selectedDevice;
        }
        set
        {
            _selectedDevice = value;
            LoadHistory();
        } 
    }

    private List<wrk_Device_ValueLog> _historyList;
    public List<wrk_Device_ValueLog> HistoryList
    {
        get { return _historyList; }
        set
        {
            _historyList = value;
            NotifyPropertyChanged();
        }
    }

    private ObservableCollection<wrk_Device_Value> _valuesList;
    public ObservableCollection<wrk_Device_Value> ValuesList
    {
        get { return _valuesList; }
        set
        {
            if (_valuesList == value) return;
            _valuesList = value;
            RaisePropertyChanged("ValuesList");
            //NotifyPropertyChanged();
        }
    }

    private void LoadValues()
    {
        DEVICE device = this.SelectedDevice;
        if (device == null) return;

        ValuesList = new ObservableCollection<wrk_Device_Value>
            (_dataContext.wrk_Device_Value.Where(d => d.ID_Device == device.ID_Device).ToList());

    }

0 个答案:

没有答案