Skip to content

Instantly share code, notes, and snippets.

@fugaku
Created February 16, 2019 10:44
Show Gist options
  • Select an option

  • Save fugaku/e5397dc19b1f417d7a6c8785476ffd31 to your computer and use it in GitHub Desktop.

Select an option

Save fugaku/e5397dc19b1f417d7a6c8785476ffd31 to your computer and use it in GitHub Desktop.
EF Core Getting Started
public class BlogsController : ControllerBase
{
private readonly AppContext _context;
public BlogsController(AppContext context)
{
_context = context;
}
// GET: api/Blogs
[HttpGet]
public async Task<ActionResult<IEnumerable<Blog>>> GetBlogs()
{
return await _context.Blogs.ToListAsync();
}
// GET: api/Blogs/5
[HttpGet("{id}")]
public async Task<ActionResult<Blog>> GetBlog(int id)
{
var blog = await _context.Blogs.FindAsync(id);
if (blog == null)
{
return NotFound();
}
return blog;
}
[HttpPost]
public async Task<ActionResult<Blog>> PostBlog(Blog blog)
{
_context.Blogs.Add(blog);
await _context.SaveChangesAsync();
return CreatedAtAction("GetBlog", new { id = blog.BlogId }, blog);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment