// src/Elecciones.Database/EleccionesDbContext.cs using Elecciones.Database.Entities; using Microsoft.EntityFrameworkCore; namespace Elecciones.Database; public class EleccionesDbContext(DbContextOptions options) : DbContext(options) { public DbSet AgrupacionesPoliticas { get; set; } public DbSet AmbitosGeograficos { get; set; } public DbSet ResultadosVotos { get; set; } public DbSet EstadosRecuentos { get; set; } public DbSet ProyeccionesBancas { get; set; } public DbSet Telegramas { get; set; } public DbSet ResumenesVotos { get; set; } public DbSet EstadosRecuentosGenerales { get; set; } public DbSet CategoriasElectorales { get; set; } public DbSet AdminUsers { get; set; } public DbSet Configuraciones { get; set; } public DbSet Bancadas { get; set; } public DbSet OcupantesBancas { get; set; } public DbSet LogosAgrupacionesCategorias { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.UseCollation("Modern_Spanish_CI_AS"); modelBuilder.Entity() .HasIndex(r => new { r.AmbitoGeograficoId, r.CategoriaId, r.AgrupacionPoliticaId }) .IsUnique(); // Precisión para los campos de porcentaje en EstadoRecuento modelBuilder.Entity(entity => { entity.HasKey(e => new { e.AmbitoGeograficoId, e.CategoriaId }); entity.Property(e => e.MesasTotalizadasPorcentaje).HasPrecision(5, 2); entity.Property(e => e.ParticipacionPorcentaje).HasPrecision(5, 2); entity.Property(e => e.VotosNulosPorcentaje).HasPrecision(18, 4); entity.Property(e => e.VotosEnBlancoPorcentaje).HasPrecision(18, 4); entity.Property(e => e.VotosRecurridosPorcentaje).HasPrecision(18, 4); }); // Precisión para el campo de porcentaje en ResultadoVoto modelBuilder.Entity() .Property(e => e.PorcentajeVotos).HasPrecision(18, 4); modelBuilder.Entity() .Property(e => e.VotosPorcentaje).HasPrecision(5, 2); modelBuilder.Entity(entity => { // Le decimos a EF que la combinación única es (AmbitoGeograficoId, CategoriaId) entity.HasKey(e => new { e.AmbitoGeograficoId, e.CategoriaId }); // Mantener la configuración de precisión entity.Property(e => e.MesasTotalizadasPorcentaje).HasPrecision(5, 2); entity.Property(e => e.ParticipacionPorcentaje).HasPrecision(5, 2); }); modelBuilder.Entity(entity => { // La combinación de ámbito, categoría y agrupación debe ser única. entity.HasIndex(p => new { p.AmbitoGeograficoId, p.CategoriaId, p.AgrupacionPoliticaId }) .IsUnique(); }); modelBuilder.Entity(entity => { // Define la relación uno a uno: una Bancada tiene un Ocupante. entity.HasOne(b => b.Ocupante) .WithOne(o => o.Bancada) .HasForeignKey(o => o.BancadaId); }); modelBuilder.Entity(entity => { // Opcional: puede definir un índice entity.HasIndex(o => o.BancadaId).IsUnique(); }); modelBuilder.Entity(entity => { entity.HasIndex(l => new { l.AgrupacionPoliticaId, l.CategoriaId }).IsUnique(); }); } }