Database Schema & Entities
Relevant source files
The following files were used as context for generating this wiki page:
- Back/Alumnos/Api/Entidades/Usuario.cs
- Back/Alumnos/Api/Entidades/Producto.cs
- Back/Alumnos/Api/Entidades/Categoria.cs
- Back/Alumnos/Api/Entidades/Mensajes.cs
- Back/Alumnos/Api/Entidades/SesionUsuario.cs
- Back/Alumnos/Api/Persistencia/AplicacionBdContexto.cs
- Back/Alumnos/Api/Comun/Interfaces/IAplicacionBdContexto.cs
- Back/Alumnos/Api/Persistencia/Configuraciones/UsuarioConfiguracion.cs
- Back/Alumnos/Api/Persistencia/Configuraciones/CategoriaConfiguracion.cs
The database layer of the project is built using Entity Framework Core, following a Code-First approach where C# classes define the database schema. The system manages a marketplace-like environment including users, product listings, categories, and messaging capabilities.
The persistence layer is centered around the AplicacionBdContexto, which implements the IAplicacionBdContexto interface. This context manages the lifecycle of entities, handles transactions, and automatically generates URL-friendly slugs for entities implementing the ISlug interface.
Sources: Back/Alumnos/Api/Persistencia/AplicacionBdContexto.cs:11-13, Back/Alumnos/Api/Comun/Interfaces/IAplicacionBdContexto.cs:5-23
Core Entities and Relationships
The system architecture is defined by five primary entities that represent the domain model.
Entity Relationship Diagram
The following diagram illustrates the structural relationships between the core database entities, highlighting the one-to-many relationship between categories and products.
Data Models Specification
User Management (Usuario)
The Usuario entity stores personal information, authentication credentials, and account status. It features a "Slug" property used for RESTful identification.
| Field | Type | Description |
|---|---|---|
| Id | int | Primary Key. |
| Nombre | string | User's first name. |
| ApellidoPaterno | string | Father's last name. |
| ApellidoMaterno | string | Mother's last name. |
| NombreUsuario | string | Unique username for login. |
| Contraseña | string | Hashed password. |
| Slug | string | URL-friendly identifier. |
| Habilitado | bool | Account status (enabled/disabled). |
Sources: Back/Alumnos/Api/Entidades/Usuario.cs:5-18, Back/Alumnos/Api/Persistencia/Configuraciones/UsuarioConfiguracion.cs:9-12
Product and Category Catalog
The catalog consists of Producto and Categoria. A category can host multiple products, and both entities support slug generation for navigation.
// Example of Slug generation logic in Categoria
public string ObtenerDescripcionParaSlug()
{
return Nombre;
}
Sources: Back/Alumnos/Api/Entidades/Categoria.cs:18-21, Back/Alumnos/Api/Entidades/Producto.cs:17-20
Persistence Infrastructure
Database Context (AplicacionBdContexto)
The AplicacionBdContexto acts as the gateway to the database. It overrides SaveChangesAsync to intercept entities and automatically populate Slugs before they are committed to the database.
Transaction Management
The system provides explicit transaction control via the IAplicacionBdContexto interface to ensure data integrity during complex operations.
- EmpezarTransaccionAsync: Begins a new transaction with
ReadCommittedisolation level. - MandarTransaccionAsync: Attempts to save changes and commit the current transaction.
- CancelarTransaccion: Performs a rollback if an error occurs during the operation.
Sources: Back/Alumnos/Api/Persistencia/AplicacionBdContexto.cs:43-85, Back/Alumnos/Api/Comun/Interfaces/IAplicacionBdContexto.cs:17-21
Entity Configurations
Entity configurations are applied using the Fluent API. Each entity has a dedicated configuration class to define constraints like Primary Keys.
- UsuarioConfiguracion: Sets
Idas the Primary Key. - CategoriaConfiguracion: Sets
CategoriaIDas the Primary Key. - MensajesConfiguracion: Sets
MensajeIdas the Primary Key.
Sources: Back/Alumnos/Api/Persistencia/Configuraciones/UsuarioConfiguracion.cs:9-12, Back/Alumnos/Api/Persistencia/Configuraciones/CategoriaConfiguracion.cs:9-12, Back/Alumnos/Api/Persistencia/Configuraciones/MensajesConfiguracion.cs:9-12
Summary
The "Database Schema & Entities" module provides a robust foundation for the application's data layer. By utilizing Entity Framework Core and a structured repository pattern via IAplicacionBdContexto, the project ensures consistent data persistence, automated identifier generation (Slugs), and reliable transaction management for the marketplace ecosystem.