170 lines
4.4 KiB
Markdown
170 lines
4.4 KiB
Markdown
# DV-004: Configuración Base de Datos
|
|
|
|
**Proyecto:** Sistema de Registro de Estudiantes
|
|
**Fecha:** 2026-01-07
|
|
|
|
---
|
|
|
|
## 1. Docker Compose (SQL Server)
|
|
|
|
```yaml
|
|
# deploy/docker/docker-compose.yml
|
|
services:
|
|
sqlserver:
|
|
image: mcr.microsoft.com/mssql/server
|
|
container_name: sqlserver-students
|
|
environment:
|
|
- ACCEPT_EULA=Y
|
|
- SA_PASSWORD=${DB_PASSWORD:-YourStrong@Passw0rd}
|
|
- MSSQL_PID=Developer
|
|
ports:
|
|
- "1433:1433"
|
|
volumes:
|
|
- sqlserver-data:/var/opt/mssql
|
|
healthcheck:
|
|
test: /opt/mssql-tools18/bin/sqlcmd -S localhost -U sa -P "$$SA_PASSWORD" -C -Q "SELECT 1"
|
|
interval: 10s
|
|
timeout: 5s
|
|
retries: 5
|
|
|
|
volumes:
|
|
sqlserver-data:
|
|
```
|
|
|
|
---
|
|
|
|
## 2. Comandos Docker
|
|
|
|
```bash
|
|
# Iniciar SQL Server
|
|
docker-compose -f deploy/docker/docker-compose.yml up -d sqlserver
|
|
|
|
# Ver logs
|
|
docker logs sqlserver-students
|
|
|
|
# Conectar a SQL Server
|
|
docker exec -it sqlserver-students /opt/mssql-tools18/bin/sqlcmd \
|
|
-S localhost -U sa -P 'YourStrong@Passw0rd' -C
|
|
|
|
# Detener
|
|
docker-compose -f deploy/docker/docker-compose.yml down
|
|
```
|
|
|
|
---
|
|
|
|
## 3. Connection String
|
|
|
|
```json
|
|
// appsettings.Development.json
|
|
{
|
|
"ConnectionStrings": {
|
|
"DefaultConnection": "Server=localhost;Database=StudentEnrollment;User Id=sa;Password=YourStrong@Passw0rd;TrustServerCertificate=True"
|
|
}
|
|
}
|
|
```
|
|
|
|
---
|
|
|
|
## 4. DbContext Configuration
|
|
|
|
```csharp
|
|
// Adapters/Driven/Persistence/Context/AppDbContext.cs
|
|
public class AppDbContext : DbContext
|
|
{
|
|
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options) { }
|
|
|
|
public DbSet<Student> Students => Set<Student>();
|
|
public DbSet<Subject> Subjects => Set<Subject>();
|
|
public DbSet<Professor> Professors => Set<Professor>();
|
|
public DbSet<Enrollment> Enrollments => Set<Enrollment>();
|
|
|
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
|
{
|
|
modelBuilder.ApplyConfigurationsFromAssembly(typeof(AppDbContext).Assembly);
|
|
}
|
|
}
|
|
```
|
|
|
|
---
|
|
|
|
## 5. Registrar DbContext
|
|
|
|
```csharp
|
|
// Adapters/Driven/Persistence/DependencyInjection.cs
|
|
public static class DependencyInjection
|
|
{
|
|
public static IServiceCollection AddPersistence(
|
|
this IServiceCollection services,
|
|
IConfiguration configuration)
|
|
{
|
|
services.AddDbContext<AppDbContext>(options =>
|
|
options.UseSqlServer(
|
|
configuration.GetConnectionString("DefaultConnection")));
|
|
|
|
services.AddScoped<IStudentRepository, StudentRepository>();
|
|
services.AddScoped<ISubjectRepository, SubjectRepository>();
|
|
services.AddScoped<IUnitOfWork, UnitOfWork>();
|
|
|
|
return services;
|
|
}
|
|
}
|
|
```
|
|
|
|
---
|
|
|
|
## 6. Migraciones
|
|
|
|
```bash
|
|
# Crear migración inicial
|
|
dotnet ef migrations add InitialCreate \
|
|
-p src/backend/Adapters.Driven.Persistence \
|
|
-s src/backend/Host
|
|
|
|
# Aplicar migración
|
|
dotnet ef database update \
|
|
-p src/backend/Adapters.Driven.Persistence \
|
|
-s src/backend/Host
|
|
|
|
# Generar script SQL
|
|
dotnet ef migrations script \
|
|
-p src/backend/Adapters.Driven.Persistence \
|
|
-s src/backend/Host \
|
|
-o database/scripts/create.sql
|
|
```
|
|
|
|
---
|
|
|
|
## 7. Seed Data
|
|
|
|
```csharp
|
|
// Adapters/Driven/Persistence/Seeding/DataSeeder.cs
|
|
public static class DataSeeder
|
|
{
|
|
public static void Seed(ModelBuilder modelBuilder)
|
|
{
|
|
// Profesores
|
|
modelBuilder.Entity<Professor>().HasData(
|
|
new { Id = 1, Name = "Dr. García" },
|
|
new { Id = 2, Name = "Dra. Martínez" },
|
|
new { Id = 3, Name = "Dr. López" },
|
|
new { Id = 4, Name = "Dra. Rodríguez" },
|
|
new { Id = 5, Name = "Dr. Hernández" }
|
|
);
|
|
|
|
// Materias (2 por profesor)
|
|
modelBuilder.Entity<Subject>().HasData(
|
|
new { Id = 1, Name = "Matemáticas I", Credits = 3, ProfessorId = 1 },
|
|
new { Id = 2, Name = "Matemáticas II", Credits = 3, ProfessorId = 1 },
|
|
new { Id = 3, Name = "Física I", Credits = 3, ProfessorId = 2 },
|
|
new { Id = 4, Name = "Física II", Credits = 3, ProfessorId = 2 },
|
|
new { Id = 5, Name = "Programación I", Credits = 3, ProfessorId = 3 },
|
|
new { Id = 6, Name = "Programación II", Credits = 3, ProfessorId = 3 },
|
|
new { Id = 7, Name = "Base de Datos I", Credits = 3, ProfessorId = 4 },
|
|
new { Id = 8, Name = "Base de Datos II", Credits = 3, ProfessorId = 4 },
|
|
new { Id = 9, Name = "Redes I", Credits = 3, ProfessorId = 5 },
|
|
new { Id = 10, Name = "Redes II", Credits = 3, ProfessorId = 5 }
|
|
);
|
|
}
|
|
}
|
|
```
|