public virtual void Delete(TEntity entityToDelete) { if (context.Entry(entityToDelete).State != EntityState.Detached) { dbSet.Attach(entityToDelete); } if (context.Entry(entityToDelete).State != EntityState.Deleted) { context.Entry(entityToDelete).State = EntityState.Deleted; } dbSet.Remove(entityToDelete); SaveChanges(); }
public void Delete(Product element) { using (StorageDbContext context = new StorageDbContext()) { context.Configuration.AutoDetectChangesEnabled = false; using (var transaction = context.Database.BeginTransaction()) { try { ConteinerRepository conteiner = new ConteinerRepository(); conteiner.DeleteAll(element); IngredientsForProductRepository recept = new IngredientsForProductRepository(); recept.Delete(element); context.Configuration.ValidateOnSaveEnabled = false; context.Products.Attach(element); context.Entry(element).State = EntityState.Deleted; context.ChangeTracker.DetectChanges(); context.SaveChanges(); transaction.Commit(); } catch (Exception) { transaction.Rollback(); throw new InvalidOperationException("Помилка видалення."); } finally { context.Configuration.ValidateOnSaveEnabled = true; } } } }
public void DeleteById(int id) { using (StorageDbContext context = new StorageDbContext()) { using (var transaction = context.Database.BeginTransaction()) { try { context.Configuration.ValidateOnSaveEnabled = false; context.Configuration.AutoDetectChangesEnabled = false; var clientForDeleting = context.Clients.First(element => element.Id == id); context.Clients.Attach(clientForDeleting); context.Entry(clientForDeleting).State = EntityState.Deleted; context.ChangeTracker.DetectChanges(); context.SaveChanges(); transaction.Commit(); } catch (Exception ex) { transaction.Rollback(); throw new Exception(ex.Message); } finally { context.Configuration.ValidateOnSaveEnabled = true; } } } }
private Product Edit(long Id, Product _updated_product) { var product = Get(Id); product = _updated_product; _db.Entry(product).State = System.Data.Entity.EntityState.Modified; _db.SaveChanges(); var updated_product = Get(Id); return(updated_product); }
public void Delete(Ingredient ingredient) { using (StorageDbContext context = new StorageDbContext()) { context.Configuration.AutoDetectChangesEnabled = false; using (var transaction = context.Database.BeginTransaction()) { try { PackageRepository package = new PackageRepository(); ProductRepository productRepository = new ProductRepository(); IngredientsForProductRepository ingredientsForProductRepository = new IngredientsForProductRepository(); package.Delete(ingredient); var allReceiptsWithEntryIngredients = ingredientsForProductRepository.GetDataSource().Where(n => n.IngredientId == ingredient.Id).ToList(); if (allReceiptsWithEntryIngredients != null) { foreach (var oneIngredientForProductInReceipt in allReceiptsWithEntryIngredients) { Product productWithEntryIngredientInReceipt = productRepository.GetDataSource().First(n => n.Id == oneIngredientForProductInReceipt.ProductId); productRepository.Delete(productWithEntryIngredientInReceipt); } } context.Configuration.ValidateOnSaveEnabled = false; context.Ingredients.Attach(ingredient); context.Entry(ingredient).State = EntityState.Deleted; context.ChangeTracker.DetectChanges(); context.SaveChanges(); transaction.Commit(); } catch (Exception) { transaction.Rollback(); throw new InvalidOperationException("Помилка видалення."); } finally { context.Configuration.ValidateOnSaveEnabled = true; } } } }
public void Remove(int index, DateTime dateRemove, int typeEvent, int amount = 1) { using (StorageDbContext context = new StorageDbContext()) { using (var transaction = context.Database.BeginTransaction()) { try { context.Configuration.AutoDetectChangesEnabled = false; context.Conteiners.Find(index).Amount -= amount; var res = context.Conteiners.Find(index); if (res.Amount < 0) { throw new ArgumentOutOfRangeException(); } if (res.Amount == 0) { var someConteiner = context.Conteiners.Find(index); context.Configuration.ValidateOnSaveEnabled = false; context.Conteiners.Attach(someConteiner); context.Entry(someConteiner).State = EntityState.Deleted; } context.ProductStatistics.Add(new ProdStatElement(res.ProductId, typeEvent, res.Weight * amount, dateRemove)); context.ChangeTracker.DetectChanges(); context.SaveChanges(); transaction.Commit(); } catch (Exception) { transaction.Rollback(); throw new ArgumentException(); } finally { context.Configuration.ValidateOnSaveEnabled = true; } } } }
public void Delete(Product product) { using (StorageDbContext context = new StorageDbContext()) { try { context.Configuration.ValidateOnSaveEnabled = false; context.Configuration.AutoDetectChangesEnabled = false; var recept = context.IngredientsForProducts.Where(element => element.ProductId == product.Id); foreach (var elementOfRecept in recept) { context.IngredientsForProducts.Attach(elementOfRecept); context.Entry(elementOfRecept).State = EntityState.Deleted; } context.ChangeTracker.DetectChanges(); context.SaveChanges(); } finally { context.Configuration.ValidateOnSaveEnabled = true; } } }
public void Remove(DateTime date1, DateTime date2) { using (var context = new StorageDbContext()) { try { context.Configuration.ValidateOnSaveEnabled = false; context.Configuration.AutoDetectChangesEnabled = false; var listOfStatistics = context.IngredientStatistics.Where(element => element.Date <= date2 && element.Date >= date1); foreach (var someConteiner in listOfStatistics) { context.IngredientStatistics.Attach(someConteiner); context.Entry(someConteiner).State = EntityState.Deleted; } context.ChangeTracker.DetectChanges(); context.SaveChanges(); } finally { context.Configuration.ValidateOnSaveEnabled = true; } } }
public void DeleteByWeight(Conteiner oneConteiner) { using (StorageDbContext context = new StorageDbContext()) { try { context.Configuration.ValidateOnSaveEnabled = false; context.Configuration.AutoDetectChangesEnabled = false; var listOfConteiner = context.IngredientsForProducts.Where(element => element.ProductId == oneConteiner.ProductId & element.Weight == oneConteiner.Weight); foreach (var someConteiner in listOfConteiner) { context.IngredientsForProducts.Attach(someConteiner); context.Entry(someConteiner).State = EntityState.Deleted; } context.ChangeTracker.DetectChanges(); context.SaveChanges(); } finally { context.Configuration.ValidateOnSaveEnabled = true; } } }
public async Task UpdateAsync(T entity) { _dbContext.Entry(entity).State = EntityState.Modified; await _dbContext.SaveChangesAsync(); }