コード例 #1
0
        public async Task <IEnumerable <Post> > GetAllAsync()
        {
            using (var context = new BlogPostDbContext(new DbContextOptions <BlogPostDbContext>()))
            {
                var posts = await context.Posts
                            .OrderBy(pst => pst.PostDate)
                            .ToListAsync();

                return(_mapper.Map <IEnumerable <PostEntity>, IEnumerable <Post> >(posts));
            }
        }
コード例 #2
0
ファイル: CommentDao.cs プロジェクト: sashalazar/BlogPost
        public async Task <Comment> AddAsync(Comment entity)
        {
            using (var context = new BlogPostDbContext(new DbContextOptions <BlogPostDbContext>()))
            {
                var postExists = await context.Set <PostEntity>().AnyAsync(x => x.Id == entity.PostId);

                if (!postExists)
                {
                    throw new InvalidOperationException($"Comment can not be added. The Post {entity.PostId} can not be found.");
                }

                var commentEntity = _mapper.Map <Comment, CommentEntity>(entity);

                var addedComment = await context.Set <CommentEntity>().AddAsync(commentEntity);

                await context.SaveChangesAsync();

                return(_mapper.Map <CommentEntity, Comment>(addedComment.Entity));
            }
        }