public async Task DeleteAsync(T entity) { if (_context.Entry(entity).State == EntityState.Detached) { _context.Set <T>().Attach(entity); } _context.Set <T>().Remove(entity); await _context.SaveChangesAsync(); }
public virtual void Add(T entity) { try { EntityEntry dbEntityEntry = _context.Entry(entity); _context.Set <T>().Add(entity); } catch (Exception ex) { throw new Exception(ex.Message); } }
public void SavePageTitle(PageTitle entity) { if (entity.Id == default) { context.Entry(entity).State = EntityState.Added; } else { context.Entry(entity).State = EntityState.Modified; } context.SaveChanges(); }
public async Task <IActionResult> PutSubCategory(int id, SubCategory subCategory) { if (id != subCategory.Id) { return(BadRequest()); } _context.Entry(subCategory).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!SubCategoryExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IActionResult> PutProduct(int id, Product product) { if (id != product.Id) { return(BadRequest()); } _context.Entry(product).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ProductExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IActionResult> PutGenre(int id, Genre genre) { if (id != genre.GenreID) { return(BadRequest()); } _context.Entry(genre).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!GenreExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IActionResult> PutColor(int id, Color color) { if (id != color.Id) { return(BadRequest()); } _context.Entry(color).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ColorExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IActionResult> PutBook(int id, Book book) { if (id != book.BookID) { return(BadRequest()); } _context.Entry(book).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!BookExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public void SaveMainSliderContent(MainSliderContent entity) { if (entity.Image == null) { entity.Image = GetMainSliderContentByIdAsNoTracking(entity.Id).Image; } context.Entry(entity).State = EntityState.Modified; context.SaveChanges(); }
}//end public void UpdateDistributor(Distributor distributor) { using (catalogDbContext = new CatalogDbContext()) { var item = catalogDbContext.Distributors.Find(distributor.DistrId); if (item != null) { catalogDbContext.Entry(item).CurrentValues.SetValues(distributor); catalogDbContext.SaveChanges(); } } }
}//end public void UpdateProduct(Product product) { using (catalogDbContext = new CatalogDbContext()) { var item = catalogDbContext.Products.Find(product.Number); if (item != null) { catalogDbContext.Entry(item).CurrentValues.SetValues(product); catalogDbContext.SaveChanges(); } } }//end
//Край на сегмента търсене--- public void UpdateClient(Client client) { using (catalogDbContext = new CatalogDbContext()) { var item = catalogDbContext.Clients.Find(client.Egn); if (item != null) { catalogDbContext.Entry(item).CurrentValues.SetValues(client); catalogDbContext.SaveChanges(); } } }//end
public void SaveCategory(Category entity) { if (entity != null) { if (entity.Image == null) { var categoty = GetCategoryById(entity.Id); entity.Image = categoty.Image; } } context.Entry(entity).State = EntityState.Modified; context.SaveChanges(); }
public Task Save(Product product, CancellationToken cancellationToken) { if (product.Id == default(int)) { _db.Products.AddAsync(product, cancellationToken); } else { _db.Entry(product).State = EntityState.Modified; } return(_db.SaveChangesAsync(cancellationToken)); }
public async Task <IActionResult> UpdateBookAsync(long id, [FromBody] BookInputViewModel bookToUpdate) { if (id <= 0) { return(BadRequest()); } var book = await _catalogContext.Books.GetDetailedBook().SingleOrDefaultAsync( b => b.Id == id); if (book == null) { return(NotFound()); } _catalogContext.Entry(book).CurrentValues.SetValues(bookToUpdate); UpdateBookAuthors(); _catalogContext.Books.Update(book); await _catalogContext.SaveChangesAsync(); return(NoContent()); // Update book authors. void UpdateBookAuthors() { var currentBookAuthorsIds = book.BooksAuthors.Select(ba => ba.AuthorId).ToArray(); var idsToRemove = currentBookAuthorsIds.Except(bookToUpdate.AuthorsIds); foreach (var aId in idsToRemove) { var toRemove = book.BooksAuthors.FirstOrDefault(b => b.AuthorId == aId); if (toRemove != null) { book.BooksAuthors.Remove(toRemove); } } var idsToAdd = bookToUpdate.AuthorsIds.Except(currentBookAuthorsIds); foreach (var aId in idsToAdd) { book.BooksAuthors.Add(new BookAuthor { AuthorId = aId, Book = book }); } } }
public async Task <IActionResult> UpdateAuthorAsync(int id, [FromBody] AuthorInputViewModel authorToUpdate) { if (id <= 0) { return(BadRequest()); } var author = await _catalogContext.Authors.SingleOrDefaultAsync( a => a.Id == id); if (author == null) { return(NotFound()); } _catalogContext.Entry(author).CurrentValues.SetValues(authorToUpdate); _catalogContext.Authors.Update(author); await _catalogContext.SaveChangesAsync(); return(NoContent()); }
public async Task <ActionResult> DeleteConfirmed(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } var deleteModel = await _db.TrainingProviders .Where(x => x.Id == id) .Select(x => new TrainingProviderViewModels.DeleteViewModel { Id = x.Id, Name = x.Name }) .SingleOrDefaultAsync(); if (deleteModel == null) { return(HttpNotFound()); } _db.Entry(new TrainingProvider { Id = deleteModel.Id }).State = EntityState.Deleted; try { await _db.SaveChangesAsync(); } catch (Exception ex) { ModelState.AddModelError("", ex.Message); return(View(deleteModel)); } return(RedirectToAction("Index")); }
public ProductItem GetProductById(Guid id) { ProductItem entity = context.ProductItems.FirstOrDefault(x => x.Id == id); context.Entry(entity).Collection(product => product.Sizes).Load(); context.Entry(entity).Collection(product => product.Images).Load(); return(entity); }
public async Task UpdateAsync(CatalogItem entity) { context.Entry(entity).State = EntityState.Modified; await context.SaveChangesAsync(); }
public void SaveDisplayedProducts(DisplayedProducts entity) { context.Entry(entity).State = EntityState.Modified; context.SaveChanges(); }
public void SaveCategoriesOfTheMonth(CategoriesOfTheMonth entity) { context.Entry(entity).State = EntityState.Modified; context.SaveChanges(); }