public static void CreateAndAddTags(T_Article article, List <T_Tag> tags) { try { using (Entities bdd = new Entities()) { using (TransactionScope scope = new TransactionScope()) { bdd.T_Article.Add(article); bdd.SaveChanges(); foreach (T_Tag tag in tags) { T_Tag oldTag = bdd.T_Tag.Where(tg => tg.Name == tag.Name).FirstOrDefault(); if (oldTag == null) { bdd.T_Tag.Add(tag); bdd.SaveChanges(); T_ArticleTag at = new T_ArticleTag() { ArticleId = article.Id, TagId = tag.Id }; bdd.T_ArticleTag.Add(at); bdd.SaveChanges(); } else { T_ArticleTag at = new T_ArticleTag() { ArticleId = article.Id, TagId = oldTag.Id }; bdd.T_ArticleTag.Add(at); bdd.SaveChanges(); } } scope.Complete(); } } } catch (Exception e) { Trace.WriteLine(e.Message); throw; } }
public static void Create(T_Tag tag) { try { using (Entities bdd = new Entities()) { bdd.T_Tag.Add(tag); bdd.SaveChanges(); } } catch (Exception e) { Trace.WriteLine(e.Message); throw; } }
public static void Update(long tagId, T_Tag tg) { try { using (Entities bdd = new Entities()) { T_Tag tag = Get(tagId); tag = tg; bdd.SaveChanges(); } } catch (Exception e) { Trace.WriteLine(e.Message); throw; } }
public static List <T_Article> SearchByTags(string tags) { try { using (Entities bdd = new Entities()) { List <T_ArticleTag> atList = T_ArticleTagCRUD.GetAll(); List <T_Article> resList = new List <T_Article>(); List <String> tagList = new List <string>(tags.Split(' ')); foreach (T_ArticleTag arta in atList) { foreach (string tag in tagList) { T_Tag curTag = DataAccess.T_TagCRUD.GetByName(tag); if (arta.TagId == curTag.Id) { Boolean present = false; foreach (T_Article elt in resList) { if (elt.Id == arta.ArticleId) { present = true; } } if (!present) { resList.Add(ArticleCRUD.Get(arta.ArticleId)); } } } } return(resList.Distinct().ToList()); } } catch (Exception e) { Trace.WriteLine(e.Message); return(new List <T_Article>()); } }