I am trying to Save Data to database.
Suppose I have a Table Named Customers
having three fields:
Id
FirstName
LastName
I have created my models using ADO.Net Entity Data Model.
Here is my ViewModel 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;
}
Here is my MainWindow.xaml file:
<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">
<Window.DataContext>
<vm:MainWindowViewModel />
</Window.DataContext>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0" Text="FirstName" />
<TextBox Grid.Row="0" Grid.Column="1" Text="{Binding FirstName, Mode=TwoWay, UpdateSourceTrigger=Explicit}" />
<TextBlock Grid.Row="1" Grid.Column="0" Text="LastName" />
<TextBox Grid.Row="1" Grid.Column="1" Text="{Binding LastName, Mode=TwoWay, UpdateSourceTrigger=Explicit}" />
<Button Grid.Row="2" Grid.Column="1" Content="Save" />
</Grid>
</Window>
I have got two problems here:
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?
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.
Update:
Suppose I have two tables like this:
Customer:
CustomerID
Name
GenderID //Foreign Key
Gender:
GenderID
Value
Now what should be the value of CurrentCustomer.Gender
in SaveCustomerChanges
Method?