public virtual void Delete(TEntity id) { if (context.Entry(id).State == EntityState.Detached) { dbSet.Attach(id); } dbSet.Remove(id); context.SaveChanges(); //TEntity deleteEntity = dbSet.Find(id); //Delete(deleteEntity); //context.SaveChanges(); }
public IActionResult Update(CategoryViewModel model) { using (var db = new EmbeddedStockContext()) { var edit = new Category { CategoryId = model.Id, Name = model.Name }; db.Categories.Attach(edit); var entry = db.Entry(edit); entry.Property(e => e.Name).IsModified = true; db.CategoryComponentTypes.RemoveRange(db.CategoryComponentTypes.Where(c => c.CategoryId == model.Id)); foreach (int id in model.ComponentTypeIds) { var type = db.ComponentTypes.Find(id); var cat = db.Categories.Find(model.Id); var cattyp = new CategoryComponentType() { Category = cat, ComponentType = type }; db.CategoryComponentTypes.Add(cattyp); } db.SaveChanges(); } //find category and update with new name and component types return(RedirectToAction("", "category", new { area = "" })); }