Exemplo n.º 1
0
        private void EditBook()
        {
            if (selectedBook != null)
            {
                var id = selectedBook.Id;
                using (var c = new Context())
                {
                    var bookForEdit = c.Books.Include("Author").Include("Genre").First(p => p.Id == id);
                    object old = bookForEdit.Clone();
                    var v = new AddBookView();
                    v.DataContext = new AddBookViewModel(bookForEdit);

                    ((AddBookViewModel)v.DataContext).BookOut += (b) =>
                    {
                        using (var t = new Context())
                        {
                            var temp = t.Books.Include("Author").Include("Genre").First(p => p.Id == id);
                            temp.PullChanges(b);
                            temp.Author = t.Authors.FirstOrDefault(p => p.Id == temp.Author.Id);
                            temp.Genre = t.Genres.FirstOrDefault(p => p.Id == temp.Genre.Id);
                            t.SaveChanges();
                        }
                        Undos.Push(new object[] { "unedit", (Book)old });
                        OnPropertyChanged("UndoIsEnabled");
                        OpenList(selectedMode);
                        v.Close();
                    };
                    v.ShowDialog();
                }
            }
        }
Exemplo n.º 2
0
        private void AddBook()
        {
            var v = new AddBookView();
            v.DataContext = new AddBookViewModel(new Book());
            ((AddBookViewModel)v.DataContext).BookOut += (b) =>
            {

                AddBookToDB(b);
                using (var c = new Context())
                {
                    Undos.Push(new object[] { "delete", c.Books.Include("Author").Include("Genre").First(p => p.Author.Name == b.Author.Name && p.Year == b.Year && p.Title == b.Title) });
                    OnPropertyChanged("UndoIsEnabled");
                }
                OpenList(selectedMode);
                v.Close();
            };
            v.ShowDialog();
        }