Conectando .NET con ApsaraDB

mteheran.dev

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

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: