示例#1
0
        public void SavePost(PostDto postDto, List <PostCategoryDto> postsCategoriesDto)
        {
            var post = _mapper.Map <Posts>(postDto);

            post.PostDate = DateTime.Now;

            var postCategories = _mapper.Map <IEnumerable <PostsCategories> >(postsCategoriesDto);

            _context.AddRange(postCategories);
            _context.Add(post);

            _context.SaveChanges();
        }
示例#2
0
        public void SavePost(PostDto postDto)
        {
            var postsCategoriesDto = new List <PostCategoryDto>();

            postDto.Id = Guid.NewGuid();

            foreach (var categoryDto in postDto.Categories)
            {
                postsCategoriesDto.Add(new PostCategoryDto {
                    Id = Guid.NewGuid(), CategoryId = categoryDto.Id, PostId = postDto.Id
                });
            }

            var post = _mapper.Map <Posts>(postDto);

            post.PostDate = DateTime.Now;

            var postCategories = _mapper.Map <IEnumerable <PostsCategories> >(postsCategoriesDto);

            _context.AddRange(postCategories);
            _context.Add(post);

            _context.SaveChanges();
        }