title: Implementing the MVVM Pattern with Entity Framework - Add Removing
body: <p>I'm trying to implement the MVVM Pattern on my WPF project that uses Entity Framework to get / manipulates data.
  I'm confused, i want to know where validating changes on my collections of model objects to database should be???
   my application is as follow : in my view i've a datagrid of persons, two textboxes that will load the name / surname of the selected person , a buton to updated changes of rows and a button to delete the selected row.</p>
  
  <p>In my ModelView , i have an observableCollection that will be loaded at the initilization of the class with information from my database ( entities) + two Relaycommands for Add/Remove buttons ( please find the code below).</p>
  
  <p>The problem is that i didn't understood well the philosophy of MVVM, where and when and how modifications on my data should be pushed to database? 
  For now, when i update a row in my database, and save my DB context modifications on my observable collection are submitted, but it's not the cdase when i remove an item, i've to manually look for it in database and remove it ( i've attached my observablecollection to a NotifyCollectionChangedEventHandler event that will handle this)... i don't get the point of using Observable collection!!!</p>
  
  <p>Is there any simple explanation of a "perfect" mvvm architecture using database data???
  Thanks! 
  my ViewModel</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Collections.ObjectModel;
  using System.Windows.Data;
  using System.ComponentModel;
  using System.Windows.Input;
  using System.Windows;
  using MVVMOK.Models;
  using MVVMOK.Base;
  using System.Collections.Specialized;
  
  namespace MVVMOK.ViewModel
  {
      class MainWindowViewModel : ViewModelBase
      {
          private DW_MargoEntities contexte;
  
  
  
          //Constructor
          public MainWindowViewModel()
          {
              contexte = new DATABASEEntities();
              collectionOfCollaborators = new ObservableCollection&lt;Collaborator&gt;();
              foreach (Collaborator c in contexte.Collaborator)
              {
                  collectionOfCollaborators.Add(c);
              }
  
  
              //Abonnement pour l'ajout ou la suppression d'éléments :
              collectionOfCollaborators.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(collectionOfCollaboratorsChanged);
  
              //Assignation des commandes : 
              this._deleteComand = new RelayCommand(new Action&lt;object&gt;(DeleteRow));
              this._updateCommand = new RelayCommand(new Action&lt;object&gt;(UpdateDB));
          }
          //liste des propriétés publiques:
  
  
          //Propriété pour représenter l'élément séléctionné du datagrid
          private  Collaborator selectedItem;
          public Collaborator _selectedItem
          {
              get { return selectedItem; }
              set
              {
                  if (value != selectedItem)
                  {
                      selectedItem = value;
                      OnPropertyChanged("_selectedItem");
                  };
              }
          }
          //Propriété pour représenter l'élément séléctionné:
          private ObservableCollection&lt;Collaborator&gt; collectionOfCollaborators;
          public ObservableCollection&lt;Collaborator&gt; _collectionOfCollaborators
          {
              get { return collectionOfCollaborators; }
              set
              {
                  this.collectionOfCollaborators = value;
                  OnPropertyChanged("_collectionOfCollaborators");
              }
          }
  
          //Commandes : 
          public ICommand _updateCommand
          {
              get;
              set;
          }
          public ICommand _deleteComand
          {
              get;
              set;
          }
  
          void collectionOfCollaboratorsChanged(object sender, NotifyCollectionChangedEventArgs e)
          {
              Collaborator f = new Collaborator();
              switch (e.Action)
              {
                  case NotifyCollectionChangedAction.Add:
  
                      for(int i = 0; i&lt;e.NewItems.Count;i++)
                      {
  
                          if (e.NewItems[i].GetType().Equals(f.GetType()))
                          {
                              contexte.Collaborator.Add(e.NewItems[i] as Collaborator);
                          }
                      }
                      contexte.SaveChanges();
                      break;
  
                  case NotifyCollectionChangedAction.Remove:
  
                      for (int i = 0; i &lt; e.OldItems.Count; i++)
                      {
  
                          if (e.OldItems[i].GetType().Equals(f.GetType()))
                          {
                              contexte.Collaborator.Remove(e.OldItems[i] as Collaborator);
                          }
                      }
                      contexte.SaveChanges();
                      break;
                  //Reset = Clear
  
              }
          }
  
  
  
          //Services :
          public void UpdateDB(object msg)
          {
              contexte.SaveChanges();
          }
  
  
          public void DeleteRow(object msg)
          {
  
              _collectionOfCollaborators.Remove(_selectedItem);
              contexte.SaveChanges();
          }
      }
  }
  </code></pre>
  
  <p>My model</p>
  
  <pre><code>namespace MVVMOK.Models
  {
      using System;
      using System.Collections.Generic;
  
      public partial class Collaborator
      {
          public int Id { get; set; }
          public string Name { get; set; }
          public string Surname { get; set; }
      }
  }
  </code></pre>
  
  <p>My XAML</p>
  
  <pre><code>&lt;Window x:Class="MVVMOK.MainWindow"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
           xmlns:local="clr-namespace:MVVMOK.ViewModel"
          Title="MainWindow" Height="350" Width="525"&gt;
      &lt;Window.DataContext&gt;
          &lt;!-- Declaratively create an instance of our SongViewModel --&gt;
          &lt;local:MainWindowViewModel /&gt;
      &lt;/Window.DataContext&gt;
      &lt;Grid&gt;
          &lt;Grid Height="237" HorizontalAlignment="Left" Margin="12,12,0,0" Name="grid1" VerticalAlignment="Top" Width="479"&gt;
              &lt;DataGrid AutoGenerateColumns="False" Height="237" HorizontalAlignment="Left" Name="dataGrid1" VerticalAlignment="Top" Width="479" ItemsSource="{Binding Path=_collectionOfCollaborators, Mode=TwoWay}" SelectionMode="Single" SelectedItem="{Binding Path=_selectedItem, Mode=TwoWay}"  &gt;
                  &lt;DataGrid.Columns&gt;
                      &lt;DataGridTextColumn Binding="{Binding Path=Id}" Header="ID" /&gt;
                      &lt;DataGridTextColumn Binding="{Binding Path=Name}" Header="Name" Width="4*" /&gt;
                  &lt;/DataGrid.Columns&gt;
              &lt;/DataGrid&gt;
          &lt;/Grid&gt;
          &lt;TextBox Height="23" HorizontalAlignment="Left" Margin="104,255,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" Text ="{Binding Path=_selectedItem.Name, Mode=TwoWay}" /&gt;
          &lt;TextBox Height="23" HorizontalAlignment="Left" Margin="104,283,0,0" Name="textBox2" VerticalAlignment="Top" Width="120" Text ="{Binding Path=_selectedItem.Surname, Mode=TwoWay}" /&gt;
          &lt;Label Content="Name" Height="28" HorizontalAlignment="Left" Margin="50,255,0,0" Name="label1" VerticalAlignment="Top" /&gt;
          &lt;Label Content="Surname" Height="28" HorizontalAlignment="Left" Margin="37,283,0,0" Name="label2" VerticalAlignment="Top" /&gt;
          &lt;Button Content="Delete Row" Height="23" HorizontalAlignment="Left" Margin="416,260,0,0" Name="button1" VerticalAlignment="Top" Width="75" Command="{Binding Path=_deleteComand}"/&gt;
          &lt;Button Content="Update All" Height="23" HorizontalAlignment="Left" Margin="335,260,0,0" Name="button2" VerticalAlignment="Top" Width="75" Command="{Binding Path=_updateCommand}"/&gt;
      &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>