1

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?

DaveO
  • 1,909
  • 4
  • 33
  • 63
  • It would if you had to change the details Collection, your masters and details collections are not linked in any way. You will need to catch the event (not sure if there is one) of the masters collection and see when a new details is being added, then you will need to update the details youself. – Jethro Jul 26 '11 at 06:22

1 Answers1

1

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.
} 
Jethro
  • 5,896
  • 3
  • 23
  • 24
  • Actually my understanding was wrong, what I was looking for was an implementation which I only realize is now available in EF4.1. That is automatically updating an ObservableCollection based of the dbcontext, as noted in Slauma's post on Local: http://stackoverflow.com/questions/6193625/observablecollection-better-than-objectset/6194617#6194617 – DaveO Jul 26 '11 at 08:26