Since you mentioned this is a ‘simple’ desktop application, there’s a chance you are creating an app that edits data by a single user. If that’s your intention, allow me to suggest an alternative to MVVM. MVVM is a very good architecture, but might be over-kill for simple desktop applications.
Here’s an alternative solution I use that works very well for a single-user app using EF Database First:
Use a single DBContext
for your main window, and all UserControls
, and have a single “Save” button/menu item to commit all changes. You can also check for changes when the user tries to exit and prompt for saving first. Also, if your objects implement INotifyPropertyChange
and ObservableCollections
, your changes in one location are reflected elsewhere in your app. There are various ways to make this DBContext
available throughout your application, such as using a Singleton pattern, or simply creating a static class and placing it in there (as a public static member). Then have all controls use this instance.
As for implementing INotifyPropertyChange
and ObservableCollections
, you have two options:
Option 1) Implement it using Database first with EntityFramework 4. This creates object inheriting from EntityObject
, which implement INotifyPropertyChange
. Also, associations are implemented as EntityCollections
, which implement AssociationChanged
. All of these are useful for simple WPF applications. You can bind your data directly to controls. This is a very simple way to create a nicely fully functional single-user desktop app. This is what I’ve done in an app I use almost daily.
Option 2) Implement with EF 6 DB first, but with modifications to support INotifyPropertyChange
and ObservableCollections
. By default, EF 6 creates POCO (plain old CLR objects) that are not by default very observable. However, the creation process can be modified in order to implement these. The KEY to this is to modify the T4 template, which is done by modifying the {yourmodelname}.tt
file. I recently did this on my EF4-based application and it’s been working well. Here are the steps to do that:
Locate your {modelname}.tt
file, make a back-up of it.
Updating the .tt
to add INotifyPropertyChange
.
See: naskew's answer here: Using INotifyPropertyChanged with Entity Framework 6 DbContext Generator,
Updating the .tt to use ObservableCollections
instead of HashSets
:
See: Kolya Evdokimov's answer here: How can I change an Entity Framework ICollection to be an ObservableCollection?,
With one change: in NavigationProperty
function of the .tt
, change the ICollection
to ObservableCollection
After changing the .tt
, right click on the .tt
file and select Run Custom Tool.
Once this is complete, your created EF classes will implement INotifyPropertyChange
, and have ObservableCollections
for associations. These should be ready for implementing DataBinding
on WPF controls. Changes in one UserControl
are reflected elsewhere in your application since the underlying model uses INotifyPropertyChange
and ObservableCollections
I'd paste my .tt
file here for you but it was too large.