Overview
The Tournament Management App uses two separate Entity Framework Core database contexts:- DataContext: Manages tournament domain entities (teams, players, matches, etc.)
- IdentityDataContext: Manages ASP.NET Core Identity user authentication and data protection keys
DataContext
TheDataContext class manages all tournament-related entities and is defined in the Torneo.App.Persistencia namespace.
Location
Torneo.App.Persistencia/DataContext.cs
DbSet Properties
The context exposes the following entity sets:Collection of municipalities where teams are located. Each municipality can have multiple teams.
Collection of technical directors (coaches). Each director can manage multiple teams.
Collection of teams participating in tournaments. Teams are associated with municipalities and directors.
Collection of matches between teams. Each match has a local (home) and visitante (away) team.
Collection of player positions (e.g., forward, midfielder, defender, goalkeeper).
Collection of players. Each player belongs to a team and has a specific position.
Entity Relationships
The domain model includes the following key relationships:- Municipio → Equipos: One-to-many (a municipality can have multiple teams)
- DirectorTecnico → Equipos: One-to-many (a director can manage multiple teams)
- Equipo → Jugadores: One-to-many (a team has multiple players)
- Equipo → Partidos: A team can be the local or visitante in multiple matches
- Posicion → Jugadores: One-to-many (a position can have multiple players)
Source Code
Torneo.App.Persistencia/DataContext.cs
Configuration
Connection String
TheDataContext reads the connection string from the DATABASE_CONNECTION_STRING environment variable. If not set, it defaults to a SQLite database at /app/Torneo.db.
Delete Behavior
All foreign key relationships are configured withDeleteBehavior.Restrict to prevent cascading deletes. This ensures referential integrity and prevents accidental data loss.
When deleting entities with related data, you must first remove or update the dependent entities to avoid foreign key constraint violations.
IdentityDataContext
TheIdentityDataContext class manages ASP.NET Core Identity user accounts and data protection keys. It inherits from IdentityDbContext<IdentityUser> and implements IDataProtectionKeyContext.
Location
Torneo.App.Frontend/Areas/Identity/Data/IdentityDataContext.cs
DbSet Properties
Collection of ASP.NET Core Data Protection keys used for encrypting authentication cookies and other sensitive data.
Inherited Tables
TheIdentityDataContext automatically includes the following ASP.NET Core Identity tables:
AspNetUsers- User accountsAspNetRoles- User rolesAspNetUserRoles- User-role mappingsAspNetUserClaims- User claimsAspNetUserLogins- External login providersAspNetUserTokens- Authentication tokensAspNetRoleClaims- Role claims
Source Code
Torneo.App.Frontend/Areas/Identity/Data/IdentityDataContext.cs
Registration in Program.cs
TheIdentityDataContext is registered in the ASP.NET Core dependency injection container during application startup:
Program.cs
Identity Configuration
The application includes additional Identity configuration for security policies:Lockout Policy
Lockout Settings
Password Policy
Password Requirements
Health Checks
The application includes health check endpoints to verify database connectivity:Health Check Configuration
http://localhost/health to verify database status.
Database Providers
The application supports multiple database providers:SQLite (Default)
SQLite
SQLite is used by default for simplicity and portability. It’s suitable for development and small-scale deployments.
SQL Server (Optional)
SQL Server
Related Documentation
Migrations
Learn how to create and apply Entity Framework migrations
Repositories
Explore the repository pattern implementation