Overview
ApiTickets uses PostgreSQL as its database engine with Entity Framework Core and the Npgsql provider for data access. The application follows a clean architecture pattern with a custom DBContext implementation.Connection String Configuration
appsettings.json Structure
The database connection string is configured in theappsettings.json file located in the API project:
appsettings.json
Connection String Parameters
PostgreSQL server host address (e.g.,
localhost, 127.0.0.1, or remote server IP)PostgreSQL database username
PostgreSQL database password
Target database name (default:
DB_tickets)The connection string name
"Conexion" is referenced throughout the application. Do not change this key unless you also update all references in the codebase.Database Context Implementation
DBContext Class
The application uses a customDBContext class that extends DbContext from Entity Framework Core:
Infrastructure/DBContext.cs
Key Features
- Constructor Injection: Connection string is passed via constructor
- Npgsql Provider: Uses
UseNpgsql()to configure PostgreSQL - DbSet Entities: Defines database tables for Users, Catalogs, and Tickets
Using DBContext in Repositories
The DBContext is instantiated in Query and Command classes by retrieving the connection string from configuration:Persistence/Queries/CatalogoQueries.cs
The application follows the Dispose pattern to properly release database connections. Each repository implements
IDisposable to ensure resources are cleaned up.Setup Steps
Install PostgreSQL
Download and install PostgreSQL from postgresql.org. Ensure the server is running on the default port 5432.
Configure Connection String
Update the
ConnectionStrings:Conexion value in appsettings.json with your PostgreSQL credentials:Required NuGet Packages
The following packages are required for database functionality:- Microsoft.EntityFrameworkCore - EF Core framework
- Npgsql.EntityFrameworkCore.PostgreSQL - PostgreSQL provider for EF Core
Entity Framework Core Features
The application leverages these EF Core capabilities:- AsNoTracking() - Read-only queries for better performance
- DbSet Collections - Strongly-typed entity access
- Async Operations - All database queries use async/await pattern
- LINQ Queries - Type-safe query composition
Troubleshooting
Connection Failed
If you encounter connection errors:- Verify PostgreSQL service is running
- Check firewall settings allow connections on port 5432
- Confirm username and password are correct
- Ensure the database
DB_ticketsexists
Authentication Failed
Update thepg_hba.conf file to allow password authentication: