public JsonResult Remove(int id) { var result = new JsonOperationResponse(); try { using (var db = new KbVaultContext()) { var currentUserId = KBVaultHelperFunctions.UserAsKbUser(User).Id; var queryParams = new SqlParameter[] { new SqlParameter("ArticleId", id) }; db.Database.ExecuteSqlCommand("Delete from ArticleTag Where ArticleId = @ArticleId", queryParams); var article = db.Articles.Single(a => a.Id == id); if (article == null) { throw new Exception(ErrorMessages.ArticleNotFound); } while (article.Attachments.Count > 0) { var a = article.Attachments.First(); KbVaultAttachmentHelper.RemoveLocalAttachmentFile(a); KbVaultLuceneHelper.RemoveAttachmentFromIndex(a); article.Attachments.Remove(a); /* * Also remove the attachment from db.attachments collection * * http://stackoverflow.com/questions/17723626/entity-framework-remove-vs-deleteobject * * If the relationship is required (the FK doesn't allow NULL values) and the relationship is not * identifying (which means that the foreign key is not part of the child's (composite) primary key) * you have to either add the child to another parent or you have to explicitly delete the child * (with DeleteObject then). If you don't do any of these a referential constraint is * violated and EF will throw an exception when you call SaveChanges - * the infamous "The relationship could not be changed because one or more of the foreign-key properties * is non-nullable" exception or similar. */ db.Attachments.Remove(a); } article.Author = currentUserId; KbVaultLuceneHelper.RemoveArticleFromIndex(article); db.Articles.Remove(article); db.SaveChanges(); result.Data = id; result.Successful = true; return(Json(result)); } } catch (Exception ex) { Log.Error(ex); result.Successful = false; result.ErrorMessage = ex.Message; return(Json(result)); } }
public async Task <IActionResult> DeleteConfirmed(long id) { var article = await _context.Articles.FindAsync(id); //************************************************************* //stari kod - vrati se while (article.Attachments.Count > 0) { var a = article.Attachments.First(); attachmentHelper.RemoveLocalAttachmentFile(a); _lucene.RemoveAttachmentFromIndex(a); article.Attachments.Remove(a); /* * Also remove the attachment from db.attachments collection * * http://stackoverflow.com/questions/17723626/entity-framework-remove-vs-deleteobject * * If the relationship is required (the FK doesn't allow NULL values) and the relationship is not * identifying (which means that the foreign key is not part of the child's (composite) primary key) * you have to either add the child to another parent or you have to explicitly delete the child * (with DeleteObject then). If you don't do any of these a referential constraint is * violated and EF will throw an exception when you call SaveChanges - * the infamous "The relationship could not be changed because one or more of the foreign-key properties * is non-nullable" exception or similar. */ _context.Attachments.Remove(a); } string currentUser = _httpContextAccessor.HttpContext.User.FindFirstValue(ClaimTypes.NameIdentifier); article.AuthorId = currentUser; //KbVaultLuceneHelper.RemoveArticleFromIndex(article); //************************************************************* _context.Articles.Remove(article); await _context.SaveChangesAsync(); //0109 - activity _activityRepository.ArticleActivities(article, "deleted"); return(RedirectToAction(nameof(Index))); }