One of our customers asked this question yesterday on the Entity Framework forums. There were a few details missing and so I am not completely sure I got the question right. But I think it is about an issue I have heard quite a bit, and so I think it may be useful to share my answer here for others.
Given this simple line of code (i.e. in a WinForms application):
Several things will happening under the hood:
When a binding list is created this way from a tracked query (i.e. MergeOption in the query is set to anything but NoTracking), there are several other interesting details in the behavior:
All these facts, especially #5, warrant me to give you a couple of tips:
We have a few ideas around things we may improve in future versions. Some of them you may be able to guess from this explanation. But I will save that for another post...
PingBack from http://www.easycoded.com/quick-tips-for-entity-framework-databinding/
Thanks for this interesting post. I have some problems with data binding and WPF. I bind two DataGrids (WPF Toolkit) to the same EntityCollection but changes in one DataGrid aren’t updated in the other one (e.g. add new item).
I believe WPF doesn’t use the IListSource.GetList() method. Anyway, it would be nice to see the EntityCollection implementing the INotifyCollectionChanged interface in EF 4.0.
We don't have plans to implement INotifyCollectionChanged in 4.0. But this is a good suggestion. Now that the interface has been moved to System.dll we can implement it without having to add a dependency on WindowsBase.dll. In the meanwhile, you can consider using your own ObservableCollections as binding lists for EF.
Take a look at Beth Massi's implementation here:
Or at C# version here:
The only detail that those samples are lacking is that they are not synchronizing the binding list with the source when changes happen in the source. You can listen to the AssociationChanged event on the EntityCollection for that.
Hope this helps,
We use a similar approach as Beth Massi. However, for RAD (Rapid Application Development) it would be nice to bind directly to the EntityCollections generated by the EDM.
I hope to see this in a future release of EF.
I have tried the bindinglist as you have specified and I have to say that you point #5 seems to be incorrect. As I read it, you are saying that adding an Entity to the IBindingList will also result in the Entity being added to the context. This is not happening. Did I misunderstand you?
Sorry I did not see your comment before. Yes, IBindingList.Add will result in the entity beging added to the context. The only case I can think of in which this is not going to happen is for the binding list of a query with MergeOption = NoTracking. Any chances that is what you are doing?
Hi Divega, what are the best practices about binding self-tracking entities to a WinForms DataGridView control?
More explicitly, I have a few of questions as follows:
1. Since TrackableCollection cannot be directly bound to the DataSource of a DataGridView, calling ToList() on a TrackableCollection and bind the result to a DataGridView seems the only way. Is this correct?
2. How is the Add/Delete/Modify operation happening on the list (from q#1) reflected into underlining TrackableCollection? Especially in the case of deletion.
3. Any advice on how to use self-tracking entities with WinForms/WPF databinding?
I hardly found quality answer on the net, so can you give me some hints? Thanks!
Im stuck up with an issue in ma application .I have created an edmx file for temporary data storage,approx 10-12 entities have been mapped to database tables .Now ,I have deleted 1 table from the database but still the application is working fine with the created entity .Can anyone tell me about its behavior.
Thanks in advance.