简体   繁体   English

首先使用EF代码保存VM属性

[英]Save VM properties with EF code first

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 然后在我的XAML中绑定到Book

<CheckBox Content="Favorite" IsChecked="{Binding Favorite}" />

How and when do I call SaveChanges() on my DatabaseContext? 如何以及何时在DatabaseContext上调用SaveChanges()?

I don't want to implement a save button. 我不想实现保存按钮。 If I intercept the PropertyChange event and call SaveChanges, nothing seems to happen. 如果我拦截PropertyChange事件并调用SaveChanges,似乎什么也没发生。 My object is modified in memory but the database does not see a change. 我的对象在内存中被修改,但是数据库看不到变化。

My BookVm class is created on application startup like so: 我的BookVm类是在应用程序启动时创建的,如下所示:

        foreach (var book in MyDbContext.Books)
            Books.Add(book);

Where Books is an ObservableCollection<Book> in my MainWindowVm 在我的MainWindowVm BooksObservableCollection<Book>

Say you had a dataservice class which returns book and update a book: 假设您有一个dataservice类,该类返回book并更​​新一本书:

 public class DataService
{
    public static void UpdateBook(Book book)
    {
        //Call the context to update the database
    }

    public static IEnumerable<Book> FetchBooks()
    {
        // return DBContext.GetBooks
    }
}

Now You can make a View Model which takes a book and then return the properties. 现在,您可以制作一个包含书本的View Model,然后返回属性。 BookId is not going to change as per database(if it is a primary key). BookId不会根据数据库进行更改(如果它是主键)。

//Domain Object
public class Book
{
    public int BookId { get; set; }
    public bool Favourite { get; set; }
}

//View Model
public class BookVm : ImplementsPropertyChanged
{
    private readonly Book book;

    //Store the book
    public BookVm(Book book)
    {
        this.book = book;
    }

    private bool favorite;
    public int BookId
    {
        get { return book.BookId; }
    }

    public bool Favorite
    {
        get { return favorite; }
        set
        {
            if (favorite != value)
            {
                favorite = value;
                //Update the database 
                UpdateDatabase();
            }
        }
    }

    private void UpdateDatabase()
    {
        //set the favorite value of the domain book
        book.Favourite = favorite;
        DataService.UpdateBook(book);
    }
}

We are storing the domain object in the ViewModel for later user. 我们将域对象存储在ViewModel中供以后的用户使用。 We can also write a controller that will display the view like this: 我们还可以编写一个控制器来显示如下视图:

public class BookController
{
    public void Display()
    {
        IEnumerable<Book> books = DataService.FetchBooks();
        ObservableCollection<BookVm> bookVms = new ObservableCollection<BookVm>();
        foreach (var book in books)
        {
            BookVm bookVm = new BookVm(book);
            bookVms.Add(bookVm);
        }
        //Get the View and then bind using the bookVms collection
    }
}

If you want to save the changes of the property Favorite you have to subscribe to the property changed event and then save the value to the DB. 如果要保存“ Favorite ”属性的更改,则必须订阅属性更改事件,然后将值保存到数据库。

Here you can find a link with information about subscription to property changed event 在这里,您可以找到一个链接,其中包含有关订阅属性更改事件的信息

Edit Here are an example from MSDN 编辑是来自MSDN的示例

Also guessing that you have similar code in the Implements property change. 还猜测您在Implements属性更改中具有类似的代码。

public class ImplementsPropertyChanged : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(string propertyName = null)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }
} 

You will also need to add the following to raise the notifications. 您还需要添加以下内容来引发通知。

public bool Favorite
    {
        get { return favorite; }
        set
        {
            if (favorite != value)
            {
                favorite = value;
                //Update the database 
                UpdateDatabase();
                OnPropertyChanged("Favorite");
            }
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM