Overview
Sistema Venta uses Entity Framework Core 7.0.1 with SQL Server as the database provider. The database context and models are code-first generated from an existing database.Database Context
DbventaContext Configuration
The main database context is located atSistemaVenta.DAL/DBContext/DbventaContext.cs:8:
SistemaVenta.DAL/DBContext/DbventaContext.cs
Connection String Registration
The context is registered inSistemaVenta.IOC/Dependencia.cs:24:
Database Schema
Entity Models
The database includes 9 main entities:Categoria - Product Categories
Categoria - Product Categories
SistemaVenta.Model/Categoria.cs
DbventaContext.cs:41):Producto - Products
Producto - Products
SistemaVenta.Model/Producto.cs
DbventaContext.cs:137):Usuario - Users
Usuario - Users
SistemaVenta.Model/Usuario.cs
DbventaContext.cs:183):Venta - Sales
Venta - Sales
SistemaVenta.Model/Venta.cs
DbventaContext.cs:216):DetalleVenta - Sale Details
DetalleVenta - Sale Details
SistemaVenta.Model/DetalleVenta.cs
DbventaContext.cs:59):Rol - User Roles
Rol - User Roles
Menu - Navigation Menu
Menu - Navigation Menu
MenuRol - Menu Permissions
MenuRol - Menu Permissions
NumeroDocumento - Document Numbers
NumeroDocumento - Document Numbers
Repository Pattern
Generic Repository
The application uses a generic repository pattern (SistemaVenta.DAL/Repositorios/GenericRepository.cs:15):
SistemaVenta.DAL/Repositorios/GenericRepository.cs
The generic repository provides:
Obtener- Get single entity by filterCrear- Create new entityEditar- Update existing entityEliminar- Delete entityConsultar- Query entities with optional filter
Specialized Repositories
For complex operations, specialized repositories extend the generic pattern:Database Setup Instructions
dotnet add package Microsoft.EntityFrameworkCore.SqlServer --version 7.0.1
dotnet add package Microsoft.EntityFrameworkCore.Tools --version 7.0.1
Execute the SQL scripts to create tables with the schema matching the entity configurations in
DbventaContext.Database Migrations
Connection String Best Practices
Development
appsettings.Development.json
Production
Use environment variables or Azure Key Vault:Docker
Entity Relationships Diagram
Querying the Database
Using Repository Pattern
Using DbContext Directly
Troubleshooting
Cannot connect to SQL Server
Cannot connect to SQL Server
Error:
A network-related or instance-specific error occurredSolutions:- Verify SQL Server is running
- Check server name is correct (
.\SQLEXPRESSorlocalhost) - Enable TCP/IP in SQL Server Configuration Manager
- Check Windows Firewall settings
- Add
TrustServerCertificate=Trueto connection string
Login failed for user
Login failed for user
Error:
Login failed for user 'NT AUTHORITY\SYSTEM'Solutions:- For Windows Authentication: Ensure the application pool identity has access
- For SQL Authentication: Verify username and password are correct
- Check SQL Server authentication mode (mixed mode vs Windows only)
Database does not exist
Database does not exist
Error:
Cannot open database "DBVENTA" requested by the loginSolution:
Create the database first:Next Steps
Configuration
Configure dependency injection and services
Deployment
Deploy the API to production