I’m using EF to connect to my database. I have a DbContext that I use to get a DbSet<MyDataModel>. I use an ObservableCollection<MyDataModel> to bind the data to a GridView in WPF. This is how I populate the collection:
ObservableCollection<MyDataModel> myCollection = new ObservableCollection<MyDataModel>(dataContect.MyDataModels);
The problem is that my database is updated from another source (another application completely) and the ObservableCollection does not detect when an item is added to the database. I don’t see how it would, but I don’t know how I can make it? The database contains a lot of entries (>10k) so I would prefer not reloading all the entries periodically..
What can I do?
Edit:
I think I was a bit unclear (and not really sure of how ObservableCollections works), but yeah, I realize now that my main question is how to refresh the DbContext when the database is updated. It doesn’t really have much to do with the ObservableCollection itself.
Seems the fundamental issue is that you need to know that something has changed that your application needs to know about.
If you have any control over the second application then you could publish a message from that to let your application know it needs to reload (or maybe even it could publish the changes themselves).
If you have no control over the other application you need to detect these yourself. You could use something like
SqlDependency, although this is not appropriate for client applications. You would need a central service to perform the monitoring and then publish a change notification.I could of course be completely wrong and there may be something built into EF to notify of changes.