Esempio n. 1
0
        /// <summary>
        /// Creates new article entity with specified tag string
        /// </summary>
        /// <param name="e">Base entity for new article entity</param>
        /// <param name="tags">String that contains tags</param>
        public void Create(DalArticle e, string tags = null)
        {
            var article = e.ToOrmArticle();

            if (tags != null)
            {
                article.Tags = new List <Tag>();
                FromTagStringToTaggs(article, tags);
            }

            context.Set <Article>().Add(article);
        }
        /// <summary>
        /// Create new article with specified tags and add it to database.
        /// </summary>
        /// <param name="article"></param>
        /// <param name="tags"></param>
        public void Create(DalArticle article, string[] tags)
        {
            if (article == null)
            {
                throw new ArgumentNullException(nameof(article));
            }
            article.Id = 0;
            article.CreationDateTime = DateTime.Now;
            Article ormArticle = article.ToOrmArticle();

            if (tags != null)
            {
                var tagsDbSet = context.Set <Tag>();
                foreach (string tag in tags)
                {
                    Tag tagEntity = tagsDbSet.FirstOrDefault(x => x.Value == tag);
                    if (tagEntity == null)//if tag not in DB -> add it
                    {
                        tagsDbSet.Add(new Tag()
                        {
                            Articles = new Article[] { ormArticle },
                            Value    = tag
                        });
                    }
                    else
                    {
                        //TODO optimize here
                        var list = tagEntity.Articles.ToList();
                        list.Add(ormArticle);
                        tagEntity.Articles = list;
                        //tagEntity.Articles.Add(ormArticle);
                    }
                } //foreach
            }     //if
            context.Set <Article>().Add(ormArticle);
        }
 public void Create(DalArticle e)
 {
     _context.Articles.Add(e.ToOrmArticle());
 }
 public void Create(DalArticle e)
 {
     _context.Articles.Add(e.ToOrmArticle());
 }