WebAPI with .Net Core and Postgres in Visual Studio Code
Get link
Facebook
X
Pinterest
Email
Other Apps
In this tutorial, you use Entity Framework Core and Postgres to create the simplest web service application in Visual Studio Code.
Create the project
First, create a folder in a spot on your machine where you want your project to live. I’m going to put mine on the ~/Documents/Workspace folder.
From Terminal, run the following commands:
mkdir TodoApi dotnet new webapi -o TodoApi code TodoApi
2. Execute the following commands
.NET Core 2.1 installs Entity Framework Core 2.1 by default. However, if you install the PostgreSQL package through NuGet package manager, it’ll install PostgreSQL 2.1.1. It results in version conflict between PostgreSQL 2.1.1 & Entity Framework Core 2.1.0.
In VS Code create a Models Folder in the root of your project.
A model is an object representing the data in your app. In this case, the only model is a to-do item. Inside of the Models folder create a TodoItem.cs class with the following code:
namespace TodoApi.Models { public class TodoItem
{
public long Id { get; set; }
public string Name { get; set; }
public bool IsComplete { get; set; } }
}
Create the database context
The database context is the main class that coordinates Entity Framework functionality for a given data model. You create this class by deriving from the Microsoft.EntityFrameworkCore.DbContext class.
Add a TodoContext.cs class in the Models folder:
using Microsoft.EntityFrameworkCore;
namespace TodoApi.Models {
public class TodoContext : DbContext
{
public TodoContext(DbContextOptions<TodoContext> options) : base(options)
{
}
public DbSet<TodoItem> TodoItems { get; set; }
}
}
Register the database context
In this step, the database context is registered with the dependency injection container. Services (such as the DB context) that are registered with the dependency injection (DI) container are available to the controllers.
Register the DB context with the service container using the built-in support for dependency injection. Replace the contents of the Startup.cs file with the following code:
using TodoApi.Models;namespace TodoApi{public class Startup{public Startup(IConfiguration configuration){Configuration = configuration;}public IConfiguration Configuration { get; }// This method gets called by the runtime. Use this method to add services to the container.public void ConfigureServices(IServiceCollection services){// Add framework services.services.AddDbContext<TodoContext>(options =>options.UseNpgsql(Configuration.GetConnectionString("DefaultConnection")));services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);}// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.public void Configure(IApplicationBuilder app, IHostingEnvironment env){if (env.IsDevelopment()){app.UseDeveloperExceptionPage();}else{app.UseHsts();}app.UseHttpsRedirection();app.UseMvc();}}}
Change Connection String
Next open appsettings.json file, in this file you will add your PostgreSQL connection string.
It’s finally time to add your migration and update your database. We are going to call our migration “initial” but you can call it whatever you like.
In the VS Code integrated terminal execute the command: dotnet ef migrations add initial
output of dotnet ef migrations add initial
This should have generated some files under a new folder in your project called Migrations
Migrations folder
Lastly to update the database execute the command: dotnet ef database update
output of database update in terminal
Your database should now be updated in PgAdmin. You may have to refresh the database to see the changes.
Database & Tables in PostgreSQL
Add a controller
In the Controllers folder, create a class named TodoController. Copy the following code:
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Linq;
using TodoApi.Models;
namespace TodoApi.Controllers {
[Route("api/[controller]")]
[ApiController]
public class TodoController : ControllerBase
{
private readonly TodoContext _context; public TodoController(TodoContext context)
{ _context = context;
if (_context.TodoItems.Count() == 0)
{
_context.TodoItems.Add(new TodoItem { Name = "Item1" }); _context.SaveChanges();
}
}
}
}
The preceding code defines an API controller class without methods. In the next sections, methods are added to implement the API. The class is annotated with an [ApiController] attribute to enable some convenient features.
The controller’s constructor uses Dependency Injection to inject the database context (TodoContext) into the controller. The database context is used in each of the CRUD methods in the controller. The constructor adds an item to the Postgres database if one doesn't exist.
Get to-do items
To get to-do items, add the following methods to the TodoController class:
[HttpGet]
public ActionResult<List<TodoItem>> GetAll()
{
return _context.TodoItems.ToList();
}
[HttpGet("{id}", Name = "GetTodo")]
public ActionResult<TodoItem> GetById(long id)
{
var item = _context.TodoItems.Find(id);
if (item == null)
{
return NotFound();
}
return item;
}
These methods implement the two GET methods:
GET /api/todo
GET /api/todo/{id}
Routing and URL paths
The [HttpGet] attribute denotes a method that responds to an HTTP GET request. The URL path for each method is constructed as follows:
Take the template string in the controller’s Route attribute:
namespace TodoApi.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class TodoController : ControllerBase
{
private readonly TodoContext _context;
[controller] is a variable which takes the controller class name minus the "Controller" suffix. For this sample, the controller class name is TodoController and the root name is "todo". ASP.NET Core routing is case insensitive.
TodoController
If the [HttpGet] attribute has a route template (such as [HttpGet("/products")], append that to the path. This sample doesn't use a template.
In the following GetById method, "{id}" is a placeholder variable for the unique identifier of the to-do item. When GetById is invoked, it assigns the value of "{id}" in the URL to the method's idparameter.
[HttpGet(“{id}”, Name = “GetTodo”)]
public ActionResult<TodoItem> GetById(long id)
{
var item = _context.TodoItems.Find(id);
if (item == null)
{
return NotFound();
}
return item;
}
Name = "GetTodo" creates a named route. Named routes:
Enable the app to create an HTTP link using the route name.
Return values
The GetAll method returns a collection of TodoItem objects. MVC automatically serializes the object to JSON and writes the JSON into the body of the response message. The response code for this method is 200, assuming there are no unhandled exceptions. Unhandled exceptions are translated into 5xx errors.
In contrast, the GetById method returns the ActionResult<T> type, which represents a wide range of return types. GetById has two different return types:
If no item matches the requested ID, the method returns a 404 error. Returning NotFound returns an HTTP 404 response.
Otherwise, the method returns 200 with a JSON response body. Returning item results in an HTTP 200 response.
Comments
Post a Comment