Good evening, I have been trying to study MVVM and CodeFirst, however I have come across a snag. I cannot seem to get my head round saving to SQL.
My example code is below, but basically I want to save a Name and an Address to my SQL database.
Please note, I already added 3 names and addresses to the table that is generated by EF. I am then populating a listview from my ViewModel. The problem is saving what the user puts in the textboxes, to the sql table.
My first guess was using command parameter, but that only allows one parameter string, and I need two.
If anyone can help me achieve this, I would really appreciate it, thanks.
View:
<Window x:Class="WPFICommandMVVM.View.Person"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Person" Height="300" Width="300">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="auto"></RowDefinition>
<RowDefinition Height="auto"></RowDefinition>
</Grid.RowDefinitions>
<Label x:Name="lblName" Content="Name" Grid.Row="0" Grid.Column="0"
VerticalAlignment="Top"></Label>
<TextBox x:Name="txtName" Grid.Row="0" Grid.Column="1" VerticalAlignment="Top"
Text="{Binding ElementName=lvPerson, Path=SelectedItem.Name}"></TextBox>
<Label x:Name="lblAddress" Content="Address" Grid.Row="1" Grid.Column="0"
VerticalAlignment="Top"></Label>
<TextBox x:Name="txtAddress" Grid.Row="1" Grid.Column="1" VerticalAlignment="Top"
Text="{Binding ElementName=lvPerson, Path=SelectedItem.Address}"></TextBox>
<Button x:Name="btnUpdate" Width="100" Height="20" VerticalAlignment="Bottom" HorizontalAlignment="Center" Grid.Row="1"
Content="Update" Command="{Binding Path=UpdateCommand}" CommandParameter="{Binding
ElementName=lvPerson, Path=SelectedItem.Address}" Margin="0,0,0,-60.8"/>
<ListView x:Name="lvPerson" Grid.Row="1" ItemsSource="{Binding Persons}" Grid.ColumnSpan="2" Margin="0,35.4,99.6,-35.8">
<ListView.View>
<GridView>
<GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}"></GridViewColumn>
<GridViewColumn Header="Address" Width="200" DisplayMemberBinding="{Binding Address}"></GridViewColumn>
</GridView>
</ListView.View>
</ListView>
</Grid>
</Grid>
</Window>
Model:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
namespace WPFICommandMVVM.Model
{
public class Person : INotifyPropertyChanged
{
private int id;
private string name;
private string address;
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propertyName)
{
if(PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public int Id
{
get
{
return id;
}
set
{
id = value;
OnPropertyChanged("Id");
}
}
public string Name
{
get
{
return name;
}
set
{
name = value;
OnPropertyChanged("Name");
}
}
public string Address
{
get
{
return address;
}
set
{
address = value;
OnPropertyChanged("Address");
}
}
}
}
ViewModel:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using WPFICommandMVVM.Model;
using WPFICommandMVVM.DataAccess;
using System.Collections.ObjectModel;
namespace WPFICommandMVVM.ViewModel
{
public class PersonViewModel
{
public IList<Person> _personList;
public IList<Person> PersonList;
ObservableCollection<Person> obVinyl = new ObservableCollection<Person>();
public PersonViewModel()
{
using (var db = new PersonContext())
{
var query = from b in db.People
orderby b.Id
select b;
foreach (var item in query)
{
obVinyl.Add(item);
PersonList = obVinyl;
}
db.SaveChanges();
}
_personList = PersonList;
}
public IList<Person> Persons
{
get { return _personList; }
set { _personList = value;}
}
private ICommand mUpdater;
public ICommand UpdateCommand
{
get
{
if (mUpdater == null)
mUpdater = new Updater();
return mUpdater;
}
set
{
mUpdater = value;
}
}
}
class Updater : ICommand
{
#region ICommand Members
public bool CanExecute(object parameter)
{
return true;
}
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public void Execute(object paramenter)
{
InsertPerson();
}
#endregion
private static void InsertPerson()
{
var person = new Person
{
};
using (var context = new PersonContext())
{
context.People.Add(person);
context.SaveChanges();
}
}
}
}
DataAccess:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using WPFICommandMVVM.Model;
namespace WPFICommandMVVM.DataAccess
{
public class PersonContext : DbContext
{
public DbSet<Person> People { get; set; }
}
}
App.xaml (Where I set the datacontext):
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;
using WPFICommandMVVM.ViewModel;
namespace WPFICommandMVVM
{
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
View.Person person = new View.Person();
PersonViewModel personViewModel = new PersonViewModel();
person.DataContext = personViewModel;
person.Show();
}
}
}
Thanks again for any help.
P.s If I am going about this wrongly, please provide examples to correct me.