基于 EF 关联的 ObservableCollection 没有更新?

发布于 2024-11-25 23:56:06 字数 724 浏览 1 评论 0原文

这是我的代码的一个非常简化的版本:

使用实体框架上下文中的集合创建集合:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

可是我不能没有你 2024-12-02 23:56:06

将事件挂钩到CollectionChanged,然后您将需要查看是否已添加详细信息(如果已添加),那么您将需要自己更新详细信息集合。

更新 :

masters.CollectionChanged += new NotifyCollectionChangedEventHandler(records_CollectionChanged);

void records_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
//e.NewItems will be an IList of all the items that were added
//You will add then find the new details and add to details collection.
} 

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 :

masters.CollectionChanged += new NotifyCollectionChangedEventHandler(records_CollectionChanged);

void records_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
//e.NewItems will be an IList of all the items that were added
//You will add then find the new details and add to details collection.
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文