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.

Advertisements

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

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

    Reply
  4. buba1947

    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

    Reply
      1. buba1947

        Thanks for your quick reply.
        where can I find the post regarding Master(Detail)-Detail(SubDetail) in your blog?
        thanks indeeed

  5. Wolfgang

    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!

    Reply
  6. best cydia sources

    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.

    Reply
  7. 4ft.me

    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.

    Reply
  8. murl.nl

    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.

    Reply
  9. Rashad

    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!

    Reply
  10. grow taller review

    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.

    Reply
  11. installous alternatives

    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.

    Reply
  12. themondayprayer.com

    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.

    Reply
  13. best blender

    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!

    Reply
  14. bleka tänderna

    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!

    Reply

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 )

Google+ photo

You are commenting using your Google+ 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 )

w

Connecting to %s