title: Add data to database using MVVM pattern
body: <p>I am trying to Save Data to database.</p>
  
  <p>Suppose I have a Table Named <code>Customers</code> having three fields:</p>
  
  <pre><code>Id
  FirstName
  LastName
  </code></pre>
  
  <p>I have created my models using ADO.Net Entity Data Model.</p>
  
  <p>Here is my ViewModel code</p>
  
  <pre><code>public class myViewModel : INotifyPropertyChanged
  {
      private string _firstName;
      public string FirstName
      {
          get
          {
              return _firstName;
          }
          set
          {
              _firstName = value;
              OnPropertyChanged("FirstName");
          }
      }
  
      private string _lastName;
      public string LastName
      {
          get
          {
              return _lastName;
          }
          set
          {
              _lastName = value;
              OnPropertyChanged("LastName");
          }
      }
  
      protected virtual void OnPropertyChanged(string PropertyName)
      {
          PropertyChangedEventHandler handler = PropertyChanged;
          if (handler != null)
          {
              handler(this, new PropertyChangedEventArgs(PropertyName));
          }
      }
  
      public event PropertyChangedEventHandler PropertyChanged;
  
  }
  </code></pre>
  
  <p>Here is my MainWindow.xaml file:</p>
  
  <pre><code>&lt;Window x:Class="Lab_Lite.MainWindow"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          xmlns:vm="clr-namespace:Lab_Lite.ViewModels"
          Title="MainWindow" Height="350" Width="525" WindowState="Maximized"&gt;
  
      &lt;Window.DataContext&gt;
          &lt;vm:MainWindowViewModel /&gt;
      &lt;/Window.DataContext&gt;
  
      &lt;Grid&gt;
          &lt;Grid.RowDefinitions&gt;
              &lt;RowDefinition Height="Auto" /&gt;
              &lt;RowDefinition Height="Auto" /&gt;
              &lt;RowDefinition Height="Auto" /&gt;
              &lt;RowDefinition Height="*" /&gt;
          &lt;/Grid.RowDefinitions&gt;
  
          &lt;Grid.ColumnDefinitions&gt;
              &lt;ColumnDefinition Width="Auto" /&gt;
              &lt;ColumnDefinition Width="*" /&gt;
          &lt;/Grid.ColumnDefinitions&gt;
  
          &lt;TextBlock Grid.Row="0" Grid.Column="0" Text="FirstName" /&gt;
          &lt;TextBox Grid.Row="0" Grid.Column="1" Text="{Binding FirstName, Mode=TwoWay, UpdateSourceTrigger=Explicit}" /&gt;
  
          &lt;TextBlock Grid.Row="1" Grid.Column="0" Text="LastName" /&gt;
          &lt;TextBox Grid.Row="1" Grid.Column="1" Text="{Binding LastName, Mode=TwoWay, UpdateSourceTrigger=Explicit}" /&gt;
  
          &lt;Button Grid.Row="2" Grid.Column="1" Content="Save" /&gt;
      &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>I have got two problems here:</p>
  
  <pre><code>1. How my ViewModel knows that FirstName property declared in ViewModel is 
     referenced to FirstName Column in my database?
  2. How to save changes to database when UpdateSourceTrigger is set to Explicit?
  </code></pre>
  
  <p>I think I have found the answer to 2nd question upto some extent using Command. But I dont know if it is right or not as I dont know the answer to my first question.</p>
  
  <p><strong>Update</strong>:</p>
  
  <p>Suppose I have two tables like this:</p>
  
  <p>Customer:</p>
  
  <pre><code>CustomerID
  Name
  GenderID //Foreign Key
  </code></pre>
  
  <p>Gender:</p>
  
  <pre><code>GenderID
  Value
  </code></pre>
  
  <p>Now what should be the value of <code>CurrentCustomer.Gender</code> in <code>SaveCustomerChanges</code> Method?</p>