public long Add(Article article, string tags, string currentUser) { _context.Articles.Add(article); _context.SaveChanges(); if (!string.IsNullOrEmpty(tags)) { //AssignTagsToArticle(article.Id, tags); AssignTagsToArticleWithAuthor(article.Id, tags, currentUser); } _context.SaveChanges(); //0109 - activity _activityRepository.ArticleActivities(article, "added"); return(article.Id); }
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))); }