Save VM properties with EF code first
- by Julien
I thought this would be simple but can't find any good examples.
public Class BookVm : ImplementsPropertyChanged
{
public int BookId {get;set;}
private bool _favorite;
public bool Favorite
{
get { return _favorite; }
set {
_favorite = value;
OnPropertyChanged("Favorite");
MyDbContext.SaveChanges() // this does not work
}
}
Then in my XAML bound to Book
<CheckBox Content="Favorite" IsChecked="{Binding Favorite}" />
How and when do I call SaveChanges() on my DatabaseContext?
I don't want to implement a save button. If I intercept the PropertyChange event and call SaveChanges, nothing seems to happen. My object is modified in memory but the database does not see a change.
My BookVm class is created on application startup like so:
foreach (var book in MyDbContext.Books)
Books.Add(book);
Where Books is an ObservableCollection<Book> in my MainWindowVm