public BookDataAccess.Books Post(BookDataAccess.Books book) { try { using (BookDBEntities entities = new BookDBEntities()) { var result = entities.Books.Add(book); entities.SaveChanges(); return(result); } } catch (Exception ex) { throw new Exception("Error cannot add this book", ex); } }
public BookDataAccess.Books Put(int id, BookDataAccess.Books book) { try { using (BookDBEntities entities = new BookDBEntities()) { var entity = entities.Books.FirstOrDefault(e => e.Id == id); if (entity == null) { throw new Exception("Error cannot update book"); } entities.Entry(entity).CurrentValues.SetValues(book); entities.SaveChanges(); return(entities.Entry(entity).Entity); } } catch (Exception ex) { throw new Exception("Cannot update book", ex); } }