public static int ArticlePvAdd(int id) { using (var blogEntities = new BlogEntities()) { var article = blogEntities.Article.First(a => a.Id == id); article.PvCount += 1; blogEntities.SaveChanges(); return article.PvCount.Value; } }
/// <summary> /// 更新博客 /// </summary> /// <param name="article"></param> /// <returns></returns> public static int UpdateArticle(Article article) { using (BlogEntities blogEntities = new BlogEntities()) { var changeArticle = blogEntities.Article.First(a => a.Id == article.Id); //changeArticle.PublicationTime = article.PublicationTime; changeArticle.Title = article.Title; changeArticle.Content = article.Content; var ArticleLabelList = article.ArticleLabel; var newLabelList =new List<ArticleLabel>(); var oldLabelList = new List<ArticleLabel>(changeArticle.ArticleLabel); foreach (var label in ArticleLabelList) { var labelFromDb = (from l in blogEntities.ArticleLabel where l.Id == label.Id select l).FirstOrDefault(); newLabelList.Add(labelFromDb); } foreach (var label in newLabelList) { if (!changeArticle.ArticleLabel.Contains(label)) { changeArticle.ArticleLabel.Add(label); } } foreach (var label in oldLabelList) { if (!newLabelList.Contains(label)) { changeArticle.ArticleLabel.Remove(label); } } blogEntities.SaveChanges(); } return article.Id; }
/// <summary> /// 删除博客 /// </summary> /// <param name="id"></param> public static void DeleteArticle(int id) { var article = GetArticle(id); if (article == null) return; using (BlogEntities blogEntities = new BlogEntities()) { blogEntities.Set<Article>().Remove(article); blogEntities.SaveChanges(); } }
/// <summary> /// 添加博客 /// </summary> /// <param name="article"></param> /// <returns></returns> public static int AddArticle(Article article) { var articleTemp = article; using (BlogEntities blogEntities = new BlogEntities()) { var ArticleLabelList = article.ArticleLabel; article.ArticleLabel = null; var newlabelList = new List<ArticleLabel>(); foreach (var label in ArticleLabelList) { var labelFromDb = (from l in blogEntities.ArticleLabel where l.Id == label.Id select l).FirstOrDefault(); newlabelList.Add(labelFromDb); } article.ArticleLabel = newlabelList; blogEntities.Article.Add(article); blogEntities.SaveChanges(); articleTemp = article; } return articleTemp.Id; }