Fix database connection handling in AlquilaFacilContext
Improved error handling when loading database connection, replaced custom context class with Dictionary<string,string>, and added null checks for safer configuration.
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
|
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
namespace Entidades;
|
namespace Entidades;
|
||||||
@@ -54,17 +55,30 @@ public partial class AlquilaFacilContext : DbContext
|
|||||||
|
|
||||||
public virtual DbSet<Venta> Ventas { get; set; }
|
public virtual DbSet<Venta> Ventas { get; set; }
|
||||||
|
|
||||||
private class context
|
|
||||||
{
|
|
||||||
public string connectiondb { get; set; } = "";
|
|
||||||
}
|
|
||||||
|
|
||||||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||||
{
|
{
|
||||||
context connection = JsonSerializer.Deserialize<context>(File.ReadAllText("settings.json")) ?? new();
|
if (!optionsBuilder.IsConfigured)
|
||||||
optionsBuilder.UseMySQL(connection.connectiondb);
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var jsonContent = File.ReadAllText("settings.json");
|
||||||
|
var options = JsonSerializer.Deserialize<Dictionary<string, string>>(jsonContent);
|
||||||
|
if (options != null && options.ContainsKey("connectiondb"))
|
||||||
|
{
|
||||||
|
optionsBuilder.UseMySQL(options["connectiondb"]);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new Exception("No se encontró la clave 'connectiondb' en el archivo settings.json");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"Error al configurar la conexión a la base de datos: {ex.Message}");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||||
{
|
{
|
||||||
modelBuilder.Entity<Canon>(entity =>
|
modelBuilder.Entity<Canon>(entity =>
|
||||||
@@ -119,6 +133,9 @@ public partial class AlquilaFacilContext : DbContext
|
|||||||
entity.Property(e => e.Email)
|
entity.Property(e => e.Email)
|
||||||
.HasMaxLength(50)
|
.HasMaxLength(50)
|
||||||
.HasColumnName("email");
|
.HasColumnName("email");
|
||||||
|
entity.Property(e => e.EmailRecuperacion)
|
||||||
|
.HasMaxLength(50)
|
||||||
|
.HasColumnName("emailRecuperacion");
|
||||||
entity.Property(e => e.Habilitado)
|
entity.Property(e => e.Habilitado)
|
||||||
.HasDefaultValueSql("b'1'")
|
.HasDefaultValueSql("b'1'")
|
||||||
.HasColumnType("bit(1)")
|
.HasColumnType("bit(1)")
|
||||||
Reference in New Issue
Block a user