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


Leave a comment