Master-details with Entity Framework in WPF by using MVVM pattern

In this short blog post we will present how to implement WPF application with Master-detail relation in Entity Framework by using MVVM pattern. We will start with creating the new Visual Studio 2010 WPF Application project, and add reference to MVVM Light Toolkit library, which you can find on  The picture below shows the starting WPF project:

Now that we have starting project, we can add the new ADO.NET Entity Framework model with NorthWind SQL database. Map the only two tables Order and OrderDetails, after that we have the following situation. If you not familiar with this procedure, more details you can find on my previous post.

The next step in implementation will be creating the ModelView of the MainWindow. The ModelView will contain implementation of all logic of the Master-detail relation. To do that point with right-click on the project, select Add-Class of the context menu item and enter the name MainWindowViewModel. If you have full instatlation of the MVVM Light Toolkit you can use MVVM template specialized for creating ModelView class.

After we create file of the MainWindowViewModel, we have to implement class and add two properties MasterCollection, and DetailsCollection.

The Next source code listing shows implementation of the MainWindowViewModel. This is minimal code for work with master – details in Entity Framework.

using GalaSoft.MvvmLight;
using System.Windows.Data;

namespace WPF_EF_MasterDetailsDemo
   public class MainWindowViewModel:ViewModelBase
      NorthwindEntities ctx = new NorthwindEntities();
      public MainWindowViewModel()

     private CollectionViewSource _entityMasterView;
     public CollectionViewSource EntityMasterView
        if (_entityMasterView == null)
        return _entityMasterView;
    public CollectionViewSource _entityDetailsView;
    public CollectionViewSource EntityDetailsView
      if (_entityDetailsView == null)
       return _entityDetailsView;
   private void GetMasterEntityCollection()
       _entityDetailsView = new CollectionViewSource();
       _entityMasterView = new CollectionViewSource();
       _entityMasterView.Source = ctx.Orders;
       _entityMasterView.View.CurrentChanged += (x, y) =>
           _entityDetailsView.Source =((Orders)_entityMasterView.View.CurrentItem).Order_Details;

After we implemented ViewModel, we have to put some GUI stuff on the MainWindow.xaml, so open the MainWindow.xaml, put two labels and two DataGrid controls in to Main Window. This looks line on the following picture:

The sample is so short that it can fit in to single image ;).

In the code-behind constructor create MainWindowViewModel and assign to Windows DataContext, and that’s it.

This post demonstrate one posible  implementation of the Master-detail relation in EF by using the MVVM pattern. The source code of this project you can find on SkyDrive.


About Bahrudin Hrnjica

PhD in Mechanical Engineering, Microsoft MVP for Visual Studio and Development Technologies. Likes .NET, Math,Data Science, Evolutionary Algorithms, Machine Learning, Blogging.

Posted on 17/01/2011, in .NET, C#, WPF and tagged , , , . Bookmark the permalink. 29 Comments.

  1. I have been working on an MVVM approach using CollectionViewSource for quite some time. This worked great! Thank you!

  2. Sir thank you so much. it helped me to understand better. can you please tell how to implement the same for Master-Detail-SubDetail scenario. I am currently working on it and I can achieve that using only EF drag and drop. But i would like to achieve it from code behind. kindly help. many thanks

  3. Many thanks for this fine explanation of the really essential subjects (using EF in MVVM)! Much better and more clear than the other results which I found over Google!

  4. In other words, if they have to create their own PHRs by inputting data
    themselves, it. First, use the dropper to coat your tonsils with salt water.
    Can you get tonsil stones if your tonsils have been extracted.

  5. I’d like to find out more? I’d like to find out
    some additional information.

  6. I have read so many articles on the topic of the
    blogger lovers however this paragraph is genuinely
    a nice piece of writing, keep it up.

  7. Ashley’s the opposite; she’s the Superiority (goal-environment) style, perhaps
    even a small exaggerated. You will be able to use your iPhone
    4 unlock the way you want and have accessibility to all
    its features.

  8. Besides the truth that their hair wasn’t the same size,
    they looked the same.

  9. You require a 3rd party software to do that. Type “” and touch on”Add Supply” to consist of this repo if you haven’t currently. Some of you can’t pay for to have something like this happen.

  10. What’s up, its goold aragraph about media print, we all be familiar with media is a enorfmous source of information.

  11. The sounds produced do not sound synthetic at all and the
    interface is also extremely easy to use. If there’s one pc manufacturer that’s renowned for its capability to produce good laptops, it’s

  12. This is also a provide you with that has been expanded.

    If you are applying in a fresh iPhone 4 then you do not have any difficulties.
    Occasionally you your investment code and secure the phone.

  13. This was extremely helpful!! Best tutorial I have found. Simple and just what I needed.

    My problem was that when the DataContext is set in the codebehind, the Bindings don’t autocomplete in Visual Studio 2010, However they work !!

  14. Users are provided with 10GB Internet use each month with the handset cost at free.
    For ordering and purchasing information see the Apple iPad website.
    Editors are usually the ones insisting on DRM.

  15. The more websites that you have your link, the more people you can get
    to register for your fan page. You must be sharp enough to generate money
    and protect your legal household. Produce your group/fan page exactly about you.

  16. Pretty! This has been an incredibly wonderful post. Thank you for
    supplying this info.

  17. Wow, superb blog structure! How long hasve you been running a bloog
    for? you made running a blog glance easy. The overall look of your
    web site is magnificent, as neatly as thhe content material!

  18. I’m not sure exacly why but this wweb site is loading very sloow for me.
    Is anyone else having this problem or is it a problem on my end?
    I’ll check back later and see if the problem still exists.

  19. You can effortlessly discover it in iTunes Application Shop.

    Now, click the blue arrow button on the bottom correct of the application. High tones of roasted, toasty malts emanate from the glass
    as I attract it only inches from my nose.

  20. You’ll need to be sure you’ve the best possible report picture!
    Ultra Defrag is definitely an additional superb software for fragmentation and optimization. Make note with this
    IP address too.

  21. Thankfulness to my fatyher who informed me about this web site, this weblog is really

  22. yoou are actually a excellent webmaster. The website loading spded iss amazing.
    It kjnd of feels that you are doing any unique trick.
    Moreover, The contents are masterpiece. you have done a wonderful activity
    on this topic!

  23. Awesome blog! Do yyou have any recommendations for aspiring writers?
    I’m hoping to start my own blog soon but I’m a little lost onn everything.
    Would you suggest starting with a free platform like WordPress or go for a paid option? There are so many options out there that I’m
    totgally confused .. Any ideas? Thanks a lot!

  24. I am actually grateful to the holder of this website who has shared this
    enormous article at at this time.

  25. I am really thankful to the owner of this website who has shared this enormous article at here.

  1. Pingback: Tweets that mention Master-details with Entity Framework in WPF by using MVVM pattern | Bahrudin Hrnjica Blog --

  2. Pingback: Entity Framework Master-detail relacija u WPF korištenjem MVVM tehnike - Blog o C++ i C#

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s