Save VM properties with EF code first
Posted
by
Julien
on Stack Overflow
See other posts from Stack Overflow
or by Julien
Published on 2014-06-08T15:01:28Z
Indexed on
2014/06/08
15:25 UTC
Read the original article
Hit count: 253
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
© Stack Overflow or respective owner