Esta es una guía en la que aprenderemos a conectar una API con ApsaraDB/postgreSQL
1. creamos una nueva API usando el comando dotnet new webapi
2. Agregamos los nugets para Entity framework y PostgreSQL
dotnet add package Microsoft.EntityFrameworkCore dotnet add package Npgsql.EntityFrameworkCore.PostgreSQL
3. Creamos la clase TodoItem, TodoItem.cs
namespace ApiPostgre; public class TodoItem { public int Id { get; set; } public string Title { get; set; } public bool IsCompleted { get; set; } }
4. Creamos TodoItemContext.cs para manejar Entity framework
using Microsoft.EntityFrameworkCore; namespace ApiPostgre; public class TodoItemContext : DbContext { public DbSet<TodoItem> TodoItems { get; set; } public TodoItemContext(DbContextOptions<TodoItemContext> options) : base(options) { } }
5. Agregamos TodoItemController incluyendo todos los mentodos (get, post, put, delete)
using Microsoft.AspNetCore.Mvc; namespace ApiPostgre.Controllers { [Route("api/[controller]")] [ApiController] public class TodoItemController : ControllerBase { TodoItemContext bd; public TodoItemController(TodoItemContext context) { bd = context; bd.Database.EnsureCreated(); } [HttpGet("")] public ActionResult<IEnumerable<TodoItem>> GetTodoItems() { return bd.TodoItems; } [HttpGet("{id}")] public…
View original post 315 more words