Пример #1
0
 public BlogPost[] GetBlogPosts()
 {
     using (BlogContext context = new BlogContext())
     {
         return context.BlogPosts.ToArray();
     }
 }
Пример #2
0
        public BlogPost UpdateBlogPost(BlogPost post)
        {
            using (BlogContext context = new BlogContext())
            {
                context.Entry(post).State = EntityState.Modified;
                context.SaveChanges();
            }

            return post;
        }
Пример #3
0
        public BlogPost CreateBlogPost(BlogPost post)
        {
            using (BlogContext context = new BlogContext())
            {
                context.BlogPosts.Add(post);
                context.SaveChanges();
            }

            return post;
        }
Пример #4
0
        public BlogPost GetBlogPost(string id)
        {
            int identifier;
            if (int.TryParse(id, out identifier))
            {
                using (BlogContext context = new BlogContext())
                {
                    return context.BlogPosts.FirstOrDefault(post => post.Id == identifier);
                }
            }

            return null;
        }
Пример #5
0
 public void DeleteBlogPost(string id)
 {
     int identifier;
     if (int.TryParse(id, out identifier))
     {
         using (BlogContext context = new BlogContext())
         {
             var entity = context.BlogPosts.FirstOrDefault(blogPost => blogPost.Id == identifier);
             if (entity != null)
             {
                 context.BlogPosts.Remove(entity);
                 context.SaveChanges();
             }
         }
     }
 }