コード例 #1
0
 public void InsertPost(Post post)
 {
     using (var context = new SsnDbContext())
     {
         context.Posts.Add(post);
         context.SaveChanges();
     }
 }
コード例 #2
0
        public Post GetPost(int postId)
        {
            using (var context = new SsnDbContext())
            {
                var query = (from p in context.Posts
                             where p.Id == postId
                             select p).Include(x => x.Comments);

                return(query.FirstOrDefault());
            }
        }
コード例 #3
0
        public List <Post> GetPostsByUser(string user)
        {
            using (var context = new SsnDbContext())
            {
                var query = (from p in context.Posts
                             where p.User == user
                             orderby p.Timestamp descending
                             select p).Include(x => x.Comments);

                return(query.ToList());
            }
        }
コード例 #4
0
        public void InsertComment(int postId, Comment comment)
        {
            using (var context = new SsnDbContext())
            {
                using (var transaction = context.Database.BeginTransaction())
                {
                    var query = from p in context.Posts
                                where p.Id == postId
                                select p;

                    Post post = query.FirstOrDefault();
                    if (post != null)
                    {
                        post.Comments.Add(comment);
                        context.SaveChanges();
                    }
                    transaction.Commit();
                }
            }
        }
コード例 #5
0
        public void UpdatePost(int postId, string content)
        {
            using (var context = new SsnDbContext())
            {
                using (var transaction = context.Database.BeginTransaction())
                {
                    var query = from p in context.Posts
                                where p.Id == postId
                                select p;

                    Post foundPost = query.FirstOrDefault();
                    if (foundPost != null)
                    {
                        foundPost.Content = content;
                        context.SaveChanges();
                        transaction.Commit();
                    }
                }
            }
        }