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 http://mvvmlight.codeplex.com/.  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
     {
       get
       {
        if (_entityMasterView == null)
          GetMasterEntityCollection();
        return _entityMasterView;
       }
    }
    public CollectionViewSource _entityDetailsView;
    public CollectionViewSource EntityDetailsView
    {
     get
     {
      if (_entityDetailsView == null)
           GetMasterEntityCollection();
       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;
         };
       _entityMasterView.View.Refresh();
   }
   }
}

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.

Advertisement

29 thoughts on “Master-details with Entity Framework in WPF by using MVVM pattern

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

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

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

  4. 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

  5. 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!

  6. You require a 3rd party software to do that. Type “http://repo666.ultrasn0w.com” 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.

  7. 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.

  8. 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.

  9. 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!

  10. 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.

  11. 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.

  12. 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.

  13. 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!

  14. 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!

Leave a Reply

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

WordPress.com Logo

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

Facebook photo

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

Connecting to %s