private void buttonSave_Click(object sender, EventArgs e) { bookModel.Tittle = textBoxBook_title.Text.Trim(); bookModel.Price = Convert.ToInt32(textBoxPrice.Text); authorModel.FirstName = textBoxAuthor_FirstName.Text.Trim(); authorModel.LatsName = textBoxAuthor_SecondName.Text.Trim(); using (LibraryEntities db = new LibraryEntities()) { if (authorModel.Id == 0 && bookModel.Id == 0) { db.Authors.Add(authorModel); db.BOOKS.Add(bookModel); } else { db.Entry(bookModel).State = System.Data.Entity.EntityState.Modified; db.Entry(authorModel).State = System.Data.Entity.EntityState.Modified; } db.SaveChanges(); Show(); } }
private void buttonDelete_Click(object sender, EventArgs e) { if (MessageBox.Show("Are you sure to delete?", "Process to delete", MessageBoxButtons.YesNo) == DialogResult.Yes) { using (LibraryEntities db = new LibraryEntities()) { var contextBook = db.Entry(bookModel); var contextAuthor = db.Entry(authorModel); if ((contextAuthor.State == System.Data.Entity.EntityState.Detached) && (contextBook.State == System.Data.Entity.EntityState.Detached)) { db.Authors.Attach(authorModel); db.BOOKS.Attach(bookModel); } db.Authors.Remove(authorModel); db.BOOKS.Remove(bookModel); db.SaveChanges(); Show(); } } }