public ActionResult Edit([Bind(Include = "Id,Name,Address")] Store store, int[] selectedProducts) { if (ModelState.IsValid) { var storeFind = db.Stores.Find(store.Id); if (storeFind != null) { storeFind.Products.Clear(); if (selectedProducts != null) { foreach (var prod in db.Products.Where(p => selectedProducts.Contains(p.Id))) { storeFind.Products.Add(prod); } } db.SaveChanges(); db.Entry(storeFind).State = EntityState.Modified; } _storeRepository.UpdateStore(store); return(RedirectToAction("Index")); } return(View(store)); }
public ActionResult Edit([Bind(Include = "Id,Name,Description")] Product product) { if (ModelState.IsValid) { db.Entry(product).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } return(View(product)); }
public void UpdateStore(Store store) { context.Entry(store).State = EntityState.Modified; context.SaveChanges(); }
public async Task <List <HierarchyId> > DeleteDetailWithDescendantsAsync( int detailId, int?parentDetailId) { List <DetailRelation> childRelationsToDelete = null; await using (var transaction = await _context.Database.BeginTransactionAsync()) { List <HierarchyId> parentHierarchyIds = null; if (parentDetailId != null) { parentHierarchyIds = await GetDetailHierarchyIdsAsync(parentDetailId.Value); } else { parentHierarchyIds = new List <HierarchyId> { HierarchyId.GetRoot() }; } var detailRelations = await _context.DetailRelations .AsNoTracking() .Where(x => x.DetailId == detailId) .ToListAsync() .ConfigureAwait(false); var ancestorRelationsToDelete = new List <DetailRelation>(); detailRelations.ForEach(detailRelation => { if (parentHierarchyIds.Contains( detailRelation.HierarchyId.GetAncestor(1))) { ancestorRelationsToDelete.Add(detailRelation); } }); var ancestorHierarchyIdsToDelete = ancestorRelationsToDelete .Select(x => x.HierarchyId) .ToList(); IQueryable <DetailRelation> childRelationsToDeleteQuery = _context.DetailRelations .Where(x => false); ancestorHierarchyIdsToDelete.ForEach(ancestorHierarchyId => { var queryPart = _context.DetailRelations .Where(x => x.HierarchyId .IsDescendantOf(ancestorHierarchyId)); childRelationsToDeleteQuery = childRelationsToDeleteQuery.Concat(queryPart); }); childRelationsToDelete = await childRelationsToDeleteQuery .ToListAsync() .ConfigureAwait(false); // To address: For some reason added DetailRelations are being tracked by the context // if use RemoveRange method, then delete query fails or don't work properly //_context.DetailRelations.RemoveRange(childRelationsToDelete); childRelationsToDelete.ForEach(x => { var entry = _context.Entry(x); entry.State = EntityState.Deleted; }); await _context.SaveChangesAsync().ConfigureAwait(false); var detailIdsOfDeletedRelations = childRelationsToDelete .Select(x => x.DetailId) .Distinct() .ToList(); var detailIdsToNotDelete = await _context.DetailRelations .Where(x => detailIdsOfDeletedRelations.Contains(x.DetailId)) .Select(x => x.DetailId) .Distinct() .ToListAsync() .ConfigureAwait(false); var detailsIdsToRemove = detailIdsOfDeletedRelations .Except(detailIdsToNotDelete) .ToList(); if (detailsIdsToRemove.Any()) { var detailsToRemove = _context.Details .Where(x => detailsIdsToRemove.Contains(x.DetailId)); _context.Details.RemoveRange(detailsToRemove); await _context.SaveChangesAsync().ConfigureAwait(false); } await transaction.CommitAsync().ConfigureAwait(false); } var resultListOfHierarchyIds = childRelationsToDelete? .Select(x => x.HierarchyId).ToList(); return(resultListOfHierarchyIds); }
public void UpdateProduct(Product product) { context.Entry(product).State = EntityState.Modified; context.SaveChanges(); }