public static bool Update <T>(T model) where T : class { BookShopContext db = new BookShopContext(); db.Entry <T>(model).State = EntityState.Modified; return(db.SaveChanges() > 0); }
public IHttpActionResult PutAuthor(int id, Author author) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != author.Id) { return(BadRequest()); } db.Entry(author).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!AuthorExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
public async Task <IActionResult> PutBook(int id, Book book) { if (id != book.Id) { return(BadRequest()); } _context.Entry(book).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!BookExists(id)) { return(NotFound()); } else { throw; } } return(Ok()); }
public ActionResult Edit([Bind(Include = "BookId,BookName,PublishedDate,AuthorName")] Book book) { if (ModelState.IsValid) { db.Entry(book).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } return(View(book)); }
public async Task <ActionResult> Edit([Bind(Include = "Id,Name,Price")] Book book) { if (ModelState.IsValid) { db.Entry(book).State = EntityState.Modified; await db.SaveChangesAsync(); return(RedirectToAction("Index")); } return(View(book)); }
public async Task <Author> GetAsync(Guid id) { var artist = await _bookShopContext.Authors .FindAsync(id); if (artist == null) { return(null); } _bookShopContext.Entry(artist).State = EntityState.Detached; return(artist); }
/// <summary> /// 更新图书 /// </summary> /// <param name="book">待添加的实体</param> /// <param name="imgList">待添加的图片</param> /// <returns>执行结果</returns> public bool UpdateBook(Product book, List <FileUpload> imgList) { bool flag = false; string bookId = book.Id.ToString(); int type = FlagMgr.Upload.SourceType.Book.ToInt(); int sourceType = FlagMgr.Upload.SourceType.Book.ToInt(); using (var cxt = new BookShopContext()) { using (DbContextTransaction transaction = cxt.Database.BeginTransaction()) { try { cxt.Entry <Product>(book).State = EntityState.Modified; flag = cxt.SaveChanges() > 0; var tempImg = cxt.FileUpload.Where(e => e.SourceId == bookId && e.SourceType == type).ToList(); if (tempImg.Count > 0) { cxt.FileUpload.RemoveRange(tempImg); cxt.SaveChanges(); } if (imgList.Count > 0) { for (int i = 0; i < imgList.Count; i++) { if (i == 0) { imgList[i].IsDefault = true; } else { imgList[i].IsDefault = false; } imgList[i].SourceType = sourceType; imgList[i].SourceId = book.Id.ToString(); } cxt.FileUpload.AddRange(imgList); cxt.SaveChanges(); } transaction.Commit(); } catch (Exception ex) { transaction.Rollback(); Console.WriteLine("Error occurred."); } } } return(flag); }
public void EditBook(int?id, Books book) { var bookEdit = db.Books.SingleOrDefault(n => n.Id == id); if (bookEdit != null) { bookEdit.NameBook = book.NameBook; bookEdit.Price = book.Price; bookEdit.Img = book.Img; bookEdit.Content = book.Content; bookEdit.Deleted = book.Deleted; bookEdit.Quantity = book.Quantity; bookEdit.Sale = book.Sale; bookEdit.Status = book.Status; db.Entry(bookEdit).State = EntityState.Modified; db.SaveChanges(); } }
public static int RemoveBooks(BookShopContext context) { int tempCount = 0; var booksToDelete = context .Books .Where(b => b.Copies < 4200) .ToList(); tempCount = booksToDelete.Count(); while (booksToDelete.Count != 0) { context.Entry(booksToDelete.First()).State = EntityState.Deleted; booksToDelete.Remove(booksToDelete.First()); } context.SaveChanges(); return(tempCount); }
public override async Task <BookModel> UpdateBook(UpdateBookRequest request, ServerCallContext context) { var book = _mapper.Map <Book>(request.Book); bool isExist = await _bookContext.Books.AnyAsync(p => p.BookId == book.BookId); if (!isExist) { throw new RpcException(new Status(StatusCode.NotFound, $"Book with Id = {request.Book.BookId} is not found.")); } _bookContext.Entry(book).State = EntityState.Modified; try { await _bookContext.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { throw; } return(_mapper.Map <BookModel>(book)); }
public Book Update(Book item) { _context.Entry(item).State = EntityState.Modified; return(item); }
public void Update(T entity) { Context.Entry(entity).State = EntityState.Modified; Context.SaveChanges(); }
public virtual void Update(TEntity entityToUpdate) { DbSet.Attach(entityToUpdate); context.Entry(entityToUpdate).State = EntityState.Modified; }
public int Update(TEntity entity) { _context.Entry(entity).State = EntityState.Modified; return(_context.SaveChanges()); }
public async Task <int> UpdateAsync(T model) { _context.Entry(model).State = EntityState.Modified; return(await _context.SaveChangesAsync()); }
public void Update(Book item) { db.Entry(item).State = EntityState.Modified; }