public void CreateArticle(ArticleEdit article) { Article new_article = new Article() { Title = article.Title, Text = article.Text, ImgName = article.ImgName, DateTime = article.DateTime }; Db.Articles.Create(new_article); using (var db = new PeriodicalContext()) { var existedArticle = db.Articles.Where(e => e.Title == new_article.Title).FirstOrDefault(); var edition = db.Editions.Where(e => e.Name == article.EditionName).FirstOrDefault(); existedArticle.Edition = edition; edition.Articles.Add(existedArticle); db.SaveChanges(); } }
public static int InsertOrUpdateArtile(Article article, int flag) { int rlt = 0; string commText = string.Empty; if (flag == 0) { commText = "insert into [tArticles] (categoryid , userid ,caption , link ,articleupdate , visible , tag ,content) values ('" + article.CategoryId + "'," + article.UserId + ",'" + article.Caption + "','" + article.Link + "','" + article.ArticleUpdate + "'," + article.IsVisible + " ,'" + article.Tag + "','" + article.Content + "')"; try { rlt = OleDbHelper.ExecuteNonQuery(commText); } catch (Exception ex) { throw ex; } } else if (flag == 1) { commText = "update [tArticles] set categoryid =" + article.CategoryId + ",userid =" + article.UserId + ",caption ='" + article.Caption + "',articleupdate ='" + article.ArticleUpdate + "' ,visible= " + article.IsVisible + " ,content= '" + article.Content + "' where articleid =" + article.ArticleId; try { rlt = OleDbHelper.ExecuteNonQuery(commText); } catch (Exception ex) { throw ex; } } return rlt; }
public void Create(ArticleDTO article) { //<Tag> tags = TagModule.ReturnTags(article.Tags); string[] newTags = TagModule.ReturnTags(article.Tags); List<Tag> tags = new List<Tag>(); if (newTags != null) { foreach (var t in newTags) { var tagFromDB = Database.Tags.GetAll().FirstOrDefault(tag => tag.Name == t); if (tagFromDB != null) tags.Add(tagFromDB); else { Tag newTag = new Tag { Name = t }; tags.Add(newTag); } } } else { tags = null; } Article art = new Article { Title = article.Title, ArticleText = article.ArticleText, CreationDate = DateTime.Now, BlogId = article.BlogId, Tags = tags }; Database.Articles.Create(art); Database.Save(); }