public BlogPost[] GetBlogPosts() { using (BlogContext context = new BlogContext()) { return context.BlogPosts.ToArray(); } }
public BlogPost UpdateBlogPost(BlogPost post) { using (BlogContext context = new BlogContext()) { context.Entry(post).State = EntityState.Modified; context.SaveChanges(); } return post; }
public BlogPost CreateBlogPost(BlogPost post) { using (BlogContext context = new BlogContext()) { context.BlogPosts.Add(post); context.SaveChanges(); } return post; }
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; }
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(); } } } }