public async Task CreateOrUpdateArticle(ArticleDto dto) { var entity = dto.To <Article>(); _articleRepository.AddOrUpdate(entity); var tagEntities = new List <Tag>(); if (dto.Tags.IsNotNullOrEmpty()) { var tags = dto.Tags.Split(new char[] { ',' }); foreach (var tag in tags) { var tagEntity = _tagRepository.Find(CmsSpecifications.TagsWithNameAndType(tag, TagType.Public)); if (tagEntity == null) { tagEntity = new Tag() { Id = Guid.NewGuid(), TagType = TagType.Public, Name = tag }; _tagRepository.Add(tagEntity); } tagEntities.Add(tagEntity); } } var associatedTags = _articleTagRepository.FindAll(CmsSpecifications.TagsWithArticleId(dto.Id)).Select(x => x.TargetId).ToList(); var tagsToBeDeleted = associatedTags.Where(x => tagEntities.Any(tag => tag.Id == x) == false); var tagsNeedToBeAdded = tagEntities.Where(x => associatedTags.Contains(x.Id) == false); if (tagsToBeDeleted.Any()) { _articleTagRepository.RemoveAll(DomainObjectSpecifications.IdIn <ArticleTag>(tagsToBeDeleted.ToList())); } if (tagsNeedToBeAdded.Any()) { foreach (var tagId in tagsNeedToBeAdded) { var association = new ArticleTag { Id = Guid.NewGuid() }; association.SourceId = dto.Id; association.TargetId = tagId.Id; _articleTagRepository.Add(association); } } if (dto.IsPublished) { _articleSEORepository.AddOrUpdate(new ArticleSEO { Id = dto.Id, Description = dto.Summary, Image = dto.Cover, Keywords = dto.Tags, Locale = dto.ContentLanguage, PageName = dto.PageName, Title = dto.Title, }); } else { if (_articleSEORepository.Exists(CmsSpecifications.ArticleSEOWithId(entity.Id))) { _articleSEORepository.Remove(new ArticleSEO { Id = entity.Id }); } } _articleRepository.Context.Commit(); }