使用EF Code-First时需要实现INotifyPropertyChanged吗? [英] Do I need to implement INotifyPropertyChanged when using EF Code-First?

查看:251
本文介绍了使用EF Code-First时需要实现INotifyPropertyChanged吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到,当使用EF CodeFirst,并通过 ObservableCollection< T> .com / en-us / library / gg696248%28v = vs.103%29.aspxrel =nofollow> DBSet.Local 属性推荐<一个href =http://msdn.microsoft.com/en-us/library/gg197521%28v=vs.103%29.aspx =nofollow>这里,UI会更新的时间实体被添加到DBContext,但是当它们被修改时不会。



似乎 DBSet.Local 没有被通知我的实体已经改变。当使用EF Code-First时,我需要为我所有的字段实现 INotifyPropertyChanged ?我没有在Code-First的任何示例中看到这一点,似乎违背了目的,但也许我错了。



我可以发布一些例子代码如果您觉得 ObservableCollection 应该接收更改通知,并且我的实体类或某些东西必须出错。



编辑所以,为了确保,如果我的ViewModel中有如下代码,那么

  private ClientContext clientContext; 
私人客户端客户端;
public Client Client
{
get {return client; }
set {
client = value;
NotifyOfPropertyChange(()=> Client);
NotifyOfPropertyChange(()=>客户端);
}
}
public ObservableCollection< Client>客户端{get {return clientContext.Clients.Local; }}

我们还假设我的View有一个Listbox,ItemSource设置为Clients,SelectedItem设置为Client 。我仍然需要在我的Model中实现INotifyPropertyChanged,即使我在ViewModel中使用它?

解决方案

是的, p>

当您修改UI的属性时,要更新UI的每个元素都必须实现INotifyPropertyChange。



原因为此,UI知道集合何时更改,但是如果其中一个元素的属性更改,则没有与之关联的CollectionChanged事件,因此UI是无知的



另请参见:实体框架CTP5代码首先,WPF - MVVM建模


I'm noticing that when using EF CodeFirst, and binding my UI to an ObservableCollection<T> via the DBSet.Local property recommended here, that the UI will update when entities are added to the DBContext but not when they are modified.

It would seem that the DBSet.Local collection is not being notified that my entities have changed. When using EF Code-First do I need to implement INotifyPropertyChanged for all of my fields? I have not seen this in any of the examples on Code-First and it seems to go against the purpose, but perhaps I'm mistaken.

I can post some example code if you feel that the ObservableCollection should be receiving change notifications and that something must be wrong in my Entity classes or something.

Edit So, just to be sure, if I have code like the following in my ViewModel

private ClientContext clientContext;
private Client client;
public Client Client
{
    get { return client; }
    set { 
          client = value; 
          NotifyOfPropertyChange(() => Client);
          NotifyOfPropertyChange(() => Clients);
        }
}
public ObservableCollection<Client> Clients { get { return clientContext.Clients.Local; }}

Let's also assume my View has a Listbox with Itemssource set to Clients, and SelectedItem set to Client. I still need to implement INotifyPropertyChanged in my Model, even though I'm using it in my ViewModel?

解决方案

Yes,

Every element that you want to update the UI when a property of it is modified must implement INotifyPropertyChange.

The reason for this is, the UI knows when the collection changes, but if the property of one of those elements changes, there is no CollectionChanged event associated with it, so the UI is ignorant

See also: Entity Framework CTP5 Code First, WPF - MVVM modeling

这篇关于使用EF Code-First时需要实现INotifyPropertyChanged吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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