基于 EF 关联的 ObservableCollection 没有更新?
这是我的代码的一个非常简化的版本:
使用实体框架上下文中的集合创建集合:
db = new MainEntities();
ObservableCollection<Master> masters = new ObservableCollection<Master>(db.Masters);
ObservableCollection<Detail> details = new ObservableCollection<Detail>(db.Details);
稍后:
m = new Master(); //create master record
d = new Detail(); //create detail record
m.Details.Add(d); //attach the detail to the master entityobject
masters.Add(m); //add to the ObservableCollection
db.SaveChanges();
这会在 db.Masters 中正确设置新的主记录; “masters”ObservableCollection 中的新主记录; db.Details 中的详细记录;但不是“详细信息”ObservableCollection 中的详细记录?
我以为 ObservableCollection 会收到这些新记录的通知?
Here's a much simplified version of my code:
Create the collection using the collection from the Entity Framework context:
db = new MainEntities();
ObservableCollection<Master> masters = new ObservableCollection<Master>(db.Masters);
ObservableCollection<Detail> details = new ObservableCollection<Detail>(db.Details);
Later on:
m = new Master(); //create master record
d = new Detail(); //create detail record
m.Details.Add(d); //attach the detail to the master entityobject
masters.Add(m); //add to the ObservableCollection
db.SaveChanges();
This correctly sets the new Master record in the db.Masters; the new Master record in the 'masters' ObservableCollection; the Detail record in db.Details; but not the Detail records in the 'details' ObservableCollection?
I thought the ObservableCollection would be notified of these new records?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将事件挂钩到
CollectionChanged
,然后您将需要查看是否已添加详细信息(如果已添加),那么您将需要自己更新详细信息集合。更新 :
Hook an event to the
CollectionChanged
then you will need to see if a Detail has been added if it has then you will need to update the details collection yourself.Update :