Click here to Skip to main content
15,921,941 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more: , +
I am using Entity Framework 6 in WPF. I have already created a Repository which returns list of entities to the Viewmodel. Since I need to bind these lists to the DataGrid, I have converted these to ObservableCollection. It needs to be done so that the change tracker of EF6 can detect the changes made, and when I call Save() on UnitOfWork, the changes are saved to the database. How can that be done?

What I have tried:

I am populating my observablecollection in ViewModel like this:
ObservableCollection<items> TestCollection = new ObservableCollection<items>(UnitOfWork.Items.GetAll());

Now, if I add any item to TestCollection and then call
common sense dictates that the item would not be added to the database. And of course, it isn't.
Updated 14-Mar-17 14:37pm
CHill60 14-Mar-17 18:49pm    
What is your actual question? We can't provide tutorials on this forum
Sabyasachi Mukherjee 14-Mar-17 20:06pm    
My actual question is "how should I keep an IEnumerable and ObservableCollection in sync?"

1 solution

Share this answer
Sabyasachi Mukherjee 14-Mar-17 20:50pm    
Thank you. The MSDN link was what I was looking for.
Graeme_Grant 14-Mar-17 20:53pm    
You are most welcome. I find that over 90% of the time when I am stuck, someone else has already solved the problem. Google search can be your best resource tool...
Sabyasachi Mukherjee 14-Mar-17 20:59pm    
Actually, none of them was exactly what I expected. I still cannot find an example of Repository pattern used with WPF. Maybe, it's because of the pitfalls of binding. Anyway, the links gave me an example of using MVVM properly, without the repository pattern.
Graeme_Grant 14-Mar-17 21:04pm    
I just had another look at the Microsoft link article and it is quite detailed with downloadable code. It has all the information that you need, you only need to adapt it to your form of use - your repository pattern in this case.
CHill60 15-Mar-17 6:14am    
You might also find this CP article useful:Repository pattern, done right[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900