2025-08-14 13:12:16 -03:00
|
|
|
// src/Elecciones.Database/EleccionesDbContext.cs
|
|
|
|
|
using Elecciones.Database.Entities;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
|
|
|
|
|
namespace Elecciones.Database;
|
|
|
|
|
|
|
|
|
|
public class EleccionesDbContext(DbContextOptions<EleccionesDbContext> options) : DbContext(options)
|
|
|
|
|
{
|
|
|
|
|
public DbSet<AgrupacionPolitica> AgrupacionesPoliticas { get; set; }
|
|
|
|
|
public DbSet<AmbitoGeografico> AmbitosGeograficos { get; set; }
|
|
|
|
|
public DbSet<ResultadoVoto> ResultadosVotos { get; set; }
|
|
|
|
|
public DbSet<EstadoRecuento> EstadosRecuentos { get; set; }
|
2025-08-15 17:31:51 -03:00
|
|
|
public DbSet<ProyeccionBanca> ProyeccionesBancas { get; set; }
|
|
|
|
|
public DbSet<Telegrama> Telegramas { get; set; }
|
|
|
|
|
public DbSet<ResumenVoto> ResumenesVotos { get; set; }
|
|
|
|
|
public DbSet<EstadoRecuentoGeneral> EstadosRecuentosGenerales { get; set; }
|
|
|
|
|
public DbSet<CategoriaElectoral> CategoriasElectorales { get; set; }
|
2025-08-29 09:54:22 -03:00
|
|
|
public DbSet<AdminUser> AdminUsers { get; set; }
|
|
|
|
|
public DbSet<Configuracion> Configuraciones { get; set; }
|
|
|
|
|
public DbSet<Bancada> Bancadas { get; set; }
|
|
|
|
|
public DbSet<OcupanteBanca> OcupantesBancas { get; set; }
|
2025-09-01 14:04:40 -03:00
|
|
|
public DbSet<LogoAgrupacionCategoria> LogosAgrupacionesCategorias { get; set; }
|
2025-09-05 11:38:25 -03:00
|
|
|
public DbSet<CandidatoOverride> CandidatosOverrides { get; set; }
|
2025-09-17 11:31:17 -03:00
|
|
|
public DbSet<Eleccion> Elecciones { get; set; }
|
2025-09-28 19:04:09 -03:00
|
|
|
public DbSet<BancaPrevia> BancasPrevias { get; set; }
|
2025-08-14 13:12:16 -03:00
|
|
|
|
|
|
|
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
|
|
|
|
{
|
2025-08-17 20:08:38 -03:00
|
|
|
base.OnModelCreating(modelBuilder);
|
2025-08-14 13:12:16 -03:00
|
|
|
|
2025-08-29 09:54:22 -03:00
|
|
|
modelBuilder.UseCollation("Modern_Spanish_CI_AS");
|
|
|
|
|
|
2025-08-14 13:12:16 -03:00
|
|
|
modelBuilder.Entity<ResultadoVoto>()
|
2025-08-23 11:35:26 -03:00
|
|
|
.HasIndex(r => new { r.AmbitoGeograficoId, r.CategoriaId, r.AgrupacionPoliticaId })
|
|
|
|
|
.IsUnique();
|
2025-08-14 13:12:16 -03:00
|
|
|
|
2025-08-17 20:08:38 -03:00
|
|
|
// Precisión para los campos de porcentaje en EstadoRecuento
|
2025-08-14 13:12:16 -03:00
|
|
|
modelBuilder.Entity<EstadoRecuento>(entity =>
|
|
|
|
|
{
|
2025-08-23 11:01:54 -03:00
|
|
|
entity.HasKey(e => new { e.AmbitoGeograficoId, e.CategoriaId });
|
2025-08-14 13:12:16 -03:00
|
|
|
entity.Property(e => e.MesasTotalizadasPorcentaje).HasPrecision(5, 2);
|
|
|
|
|
entity.Property(e => e.ParticipacionPorcentaje).HasPrecision(5, 2);
|
2025-08-17 20:08:38 -03:00
|
|
|
entity.Property(e => e.VotosNulosPorcentaje).HasPrecision(18, 4);
|
|
|
|
|
entity.Property(e => e.VotosEnBlancoPorcentaje).HasPrecision(18, 4);
|
|
|
|
|
entity.Property(e => e.VotosRecurridosPorcentaje).HasPrecision(18, 4);
|
2025-08-14 13:12:16 -03:00
|
|
|
});
|
2025-08-17 20:08:38 -03:00
|
|
|
|
|
|
|
|
// Precisión para el campo de porcentaje en ResultadoVoto
|
|
|
|
|
modelBuilder.Entity<ResultadoVoto>()
|
|
|
|
|
.Property(e => e.PorcentajeVotos).HasPrecision(18, 4);
|
|
|
|
|
|
2025-08-15 17:31:51 -03:00
|
|
|
modelBuilder.Entity<ResumenVoto>()
|
2025-08-17 20:08:38 -03:00
|
|
|
.Property(e => e.VotosPorcentaje).HasPrecision(5, 2);
|
2025-08-15 17:31:51 -03:00
|
|
|
|
|
|
|
|
modelBuilder.Entity<EstadoRecuentoGeneral>(entity =>
|
2025-08-29 09:54:22 -03:00
|
|
|
{
|
|
|
|
|
// Le decimos a EF que la combinación única es (AmbitoGeograficoId, CategoriaId)
|
|
|
|
|
entity.HasKey(e => new { e.AmbitoGeograficoId, e.CategoriaId });
|
2025-08-17 20:08:38 -03:00
|
|
|
|
2025-08-29 09:54:22 -03:00
|
|
|
// Mantener la configuración de precisión
|
|
|
|
|
entity.Property(e => e.MesasTotalizadasPorcentaje).HasPrecision(5, 2);
|
|
|
|
|
entity.Property(e => e.ParticipacionPorcentaje).HasPrecision(5, 2);
|
|
|
|
|
});
|
2025-08-23 12:27:27 -03:00
|
|
|
modelBuilder.Entity<ProyeccionBanca>(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();
|
|
|
|
|
});
|
2025-08-29 09:54:22 -03:00
|
|
|
modelBuilder.Entity<Bancada>(entity =>
|
|
|
|
|
{
|
|
|
|
|
// Define la relación uno a uno: una Bancada tiene un Ocupante.
|
|
|
|
|
entity.HasOne(b => b.Ocupante)
|
|
|
|
|
.WithOne(o => o.Bancada)
|
|
|
|
|
.HasForeignKey<OcupanteBanca>(o => o.BancadaId);
|
|
|
|
|
});
|
|
|
|
|
modelBuilder.Entity<OcupanteBanca>(entity =>
|
|
|
|
|
{
|
|
|
|
|
// Opcional: puede definir un índice
|
|
|
|
|
entity.HasIndex(o => o.BancadaId).IsUnique();
|
|
|
|
|
});
|
2025-09-01 14:04:40 -03:00
|
|
|
modelBuilder.Entity<LogoAgrupacionCategoria>(entity =>
|
|
|
|
|
{
|
2025-09-02 15:39:17 -03:00
|
|
|
// La combinación de las tres columnas debe ser única.
|
|
|
|
|
entity.HasIndex(l => new { l.AgrupacionPoliticaId, l.CategoriaId, l.AmbitoGeograficoId }).IsUnique();
|
2025-09-01 14:04:40 -03:00
|
|
|
});
|
2025-09-05 11:38:25 -03:00
|
|
|
modelBuilder.Entity<CandidatoOverride>(entity =>
|
|
|
|
|
{
|
|
|
|
|
// La combinación de agrupación, categoría y ámbito debe ser única
|
|
|
|
|
// para evitar tener dos nombres de candidato diferentes para la misma situación.
|
|
|
|
|
entity.HasIndex(c => new { c.AgrupacionPoliticaId, c.CategoriaId, c.AmbitoGeograficoId })
|
|
|
|
|
.IsUnique();
|
|
|
|
|
});
|
2025-09-28 19:04:09 -03:00
|
|
|
modelBuilder.Entity<BancaPrevia>(entity =>
|
|
|
|
|
{
|
|
|
|
|
entity.Property(e => e.AgrupacionPoliticaId)
|
|
|
|
|
.UseCollation("Modern_Spanish_CI_AS");
|
|
|
|
|
});
|
2025-08-14 13:12:16 -03:00
|
|
|
}
|
|
|
|
|
}
|