Overview
TechCore uses Entity Framework Core 10.0.3 as its Object-Relational Mapper (ORM) with SQL Server as the database provider. The data access layer is centralized in theTechCoreContext class.
TechCoreContext
Location:~/workspace/source/TechCore/Datos/TechCoreContext.cs
The TechCoreContext is a partial class that inherits from DbContext and manages all database operations.
Class Definition
DbContext Registration
The context is registered inProgram.cs with dependency injection:
DbSet Collections
The context exposes 13 entity tables and 3 database views:Core Entities
Transaction Entities
Database Views
Model Configuration
Entity configurations are defined in theOnModelCreating method using the Fluent API.
Primary Keys
Keys are configured with explicit constraint names:Table Mapping
Entities map to lowercase table names:Database Triggers
Some tables have triggers registered:Indexes
The context defines numerous indexes for query performance optimization:Unique Indexes
Filtered Indexes
Composite Indexes
Common Query Indexes
Column Mappings
Column properties are configured with data types, constraints, and default values:Data Types
Default Values
Relationships
One-to-Many Relationships
Venta → Cliente
Producto → Categorium
User → Rol
Master-Detail Relationships
Venta → VentasDetalle
Compra → ComprasDetalle
Complex Relationships
Venta (has multiple related entities)
Delete Behaviors
ClientSetNull (default)
No explicit delete behavior
Database Views
Views are configured withHasNoKey() and ToView():
Overdue Installments View
Account Status View
Partial Class Pattern
TheTechCoreContext uses the partial class pattern:
- Code regeneration - EF Core scaffolding can regenerate the main class
- Custom extensions - Developers can add custom configuration in a separate partial class file
- Separation of concerns - Auto-generated code vs. manual customizations
Usage in Controllers
Inject the context via dependency injection:Best Practices
- Always use async methods -
ToListAsync(),FirstOrDefaultAsync(),SaveChangesAsync() - Use navigation properties - Leverage EF Core’s relationship tracking
- Apply filters early - Use
Where()before loading data - Use Include for eager loading - Avoid N+1 query problems
- Dispose contexts properly - Let dependency injection handle lifecycle
Next Steps
- Explore the Entity Models
- Learn about Application Architecture