Skip to main content

Overview

The Tournament Management App uses Entity Framework Core with support for both SQLite (default) and SQL Server databases. The application maintains two separate database contexts for different purposes.

Database Contexts

The application uses two Entity Framework Core contexts:

DataContext

Purpose: Tournament domain dataEntities:
  • Municipios (Municipalities)
  • DirectoresTecnicos (Technical Directors)
  • Equipos (Teams)
  • Jugadores (Players)
  • Partidos (Matches)
  • Posiciones (Positions)

IdentityDataContext

Purpose: User authenticationEntities:
  • AspNetUsers
  • AspNetRoles
  • AspNetUserClaims
  • AspNetRoleClaims
  • DataProtectionKeys

Database Providers

SQLite (Default)

SQLite is the default database provider, ideal for development and small deployments: Advantages:
  • No separate database server required
  • File-based storage
  • Zero configuration
  • Perfect for development and testing
Connection String:
Program.cs
The database file is created automatically at /app/Torneo.db inside the Docker container, or in the application directory for local development.

SQL Server

For production deployments with higher load, you can use SQL Server: Advantages:
  • Better performance at scale
  • Advanced features (stored procedures, full-text search)
  • Enterprise support and tooling
  • Concurrent access handling
Configuration:
1

Set environment variable

2

Update DbContext configuration

The application automatically detects SQL Server connection strings. No code changes needed.
3

Run migrations

Initial Setup

Docker Deployment (Automatic)

When using Docker, migrations run automatically during image build:
Dockerfile (lines 19-35)
The database is fully initialized when the container starts.

Local Development (Manual)

For local development without Docker:
1

Install Entity Framework Tools

Verify installation:
2

Navigate to the repository

3

Create DataContext migrations

4

Apply DataContext migrations

5

Create IdentityDataContext migrations

6

Apply IdentityDataContext migrations

7

Run the application

The application will start on https://localhost:5001 and http://localhost:5000.

Database Schema

Tournament Domain Tables

Identity Tables

Entity Relationships

Migration Management

List Migrations

View applied and pending migrations:

Create New Migration

When you modify entity models:

Rollback Migration

To undo the last migration:
Rollbacks can cause data loss if the migration being removed includes DropTable or DropColumn operations. Always backup your database before rolling back migrations.

Generate SQL Script

To review SQL without applying changes:

Database Seeding

To add initial data, you can seed the database in DataContext.cs using the OnModelCreating method:
Example Seeding
After adding seed data, create a new migration:

Health Checks

The application includes database health checks configured in Program.cs:
Program.cs (lines 43-44)
Check database health:
Expected Response:

Backup and Restore

SQLite Backup

For SQLite databases, simply copy the database file:

SQL Server Backup

For SQL Server, use standard backup commands:

Troubleshooting

Solution: Ensure you specify the correct context and projects:
Always use the full namespace for the context.
Solution: SQLite doesn’t handle concurrent writes well. Ensure:
  • Only one process accesses the database at a time
  • Close all connections properly
  • For high-concurrency scenarios, use SQL Server instead
Solution: The application uses DeleteBehavior.Restrict to prevent cascading deletes. You must:
  1. Delete dependent records first (e.g., delete Jugadores before deleting Equipo)
  2. Or update relationships to null before deletion
See Data Context for relationship configurations.
Solution: Set the DATABASE_CONNECTION_STRING environment variable:
Or update appsettings.json:
Solution: Install Entity Framework Core tools globally:
Add to PATH if needed:

Next Steps

Data Context API

Explore the DataContext and IdentityDataContext APIs

Migrations Guide

Learn about Entity Framework Core migrations

Configuration

Configure environment variables and settings

Domain Models

Review the entity models and relationships