144 lines
4.6 KiB
C#
144 lines
4.6 KiB
C#
//using bdEmtusa.CompiledModels;
|
|
using bdEmtusa.db;
|
|
using bdEmtusa.dbcontext;
|
|
|
|
using Microsoft.AspNetCore.Mvc.Formatters;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.EntityFrameworkCore.Metadata;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Hosting;
|
|
using Microsoft.Extensions.Logging;
|
|
using Microsoft.Extensions.Options;
|
|
using Newtonsoft.Json;
|
|
using System.ComponentModel.DataAnnotations;
|
|
using System.Diagnostics;
|
|
using System.Reflection;
|
|
using System.Reflection.Emit;
|
|
using System.Runtime.CompilerServices;
|
|
using tsEFCore8.Extensiones;
|
|
using tsUtilidades;
|
|
using tsUtilidades.Enumeraciones;
|
|
using tsUtilidades.Extensiones;
|
|
using static System.Net.Mime.MediaTypeNames;
|
|
|
|
namespace bdEmtusa
|
|
{
|
|
public class tscEmtusa : bdEmtusa.dbcontext.EmtusaContext, tsUtilidades.ItsContexto
|
|
|
|
{
|
|
|
|
public static bool Cargado = false;
|
|
private static String? _Ip = null;
|
|
public string? ip
|
|
{
|
|
get
|
|
{
|
|
return _Ip;
|
|
}
|
|
set
|
|
{
|
|
_Ip = value;
|
|
}
|
|
}
|
|
|
|
public string Aplicaciones { get; private set; }
|
|
|
|
public static readonly Microsoft.Extensions.Logging.LoggerFactory _myLoggerFactory =
|
|
new LoggerFactory(new[] {
|
|
new Microsoft.Extensions.Logging.Debug.DebugLoggerProvider()
|
|
});
|
|
|
|
private static string? ConexionPorDefecto = null;
|
|
public static tscEmtusa NuevoContexto(string NombreConexion = "", bool Lazy = true, bool SoloLectura = false, bool ConEventoSavingChanges = true, string aplicaciones = "")
|
|
{
|
|
|
|
|
|
|
|
string? cnx = null;
|
|
if (NombreConexion == "")
|
|
{
|
|
if (ConexionPorDefecto == null) ConexionPorDefecto = Conexion.ObtieneConexionDefecto();
|
|
cnx = ConexionPorDefecto;
|
|
}
|
|
else
|
|
{
|
|
cnx = Conexion.ObtieneConexionDefecto(NombreConexion);
|
|
}
|
|
|
|
var ob = new DbContextOptionsBuilder<EmtusaContext>();
|
|
// ob.UseLoggerFactory(_myLoggerFactory);
|
|
// ob.UseInternalServiceProvider(<EnumeracionesService>)
|
|
ob.UseMySql(cnx, Microsoft.EntityFrameworkCore.ServerVersion.Parse("8.0.0-mysql"));
|
|
if (Lazy) ob.UseLazyLoadingProxies();
|
|
if (SoloLectura) ob.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);
|
|
var Opciones = ob.Options;
|
|
tscEmtusa bd = new tscEmtusa(Opciones);
|
|
bd.Aplicaciones = aplicaciones;
|
|
if (ConEventoSavingChanges) bd.SavingChanges += GuardandoCambios;
|
|
return bd;
|
|
|
|
}
|
|
|
|
private static void CambiosGuardados(object? sender, SavedChangesEventArgs e)
|
|
{
|
|
|
|
}
|
|
|
|
//public static Datos.BBDD ObtieneBBDD(string NombreConexion)
|
|
|
|
private static void GuardandoCambios(object? sender, SavingChangesEventArgs e)
|
|
{
|
|
|
|
|
|
}
|
|
|
|
public tscEmtusa(DbContextOptions<EmtusaContext> Opciones) : base(Opciones)
|
|
{
|
|
if (_Ip == null) { ip = tsEFCore8.bbdd.ObtieneIPMysql(this); }
|
|
else { ip = _Ip; }
|
|
}
|
|
|
|
|
|
|
|
public void AñadeObjeto(object Registro)
|
|
{
|
|
this.Add(Registro);
|
|
}
|
|
|
|
public bool CompruebaUnico(EstadosAplicacion estado, string NombreCampo, object Valor, string NombreTablaBase, object DataContext)
|
|
{
|
|
return this.CompruebaRegistroUnico(estado == EstadosAplicacion.ModificandoRegistro, "bdEmtusa.db", NombreTablaBase, NombreCampo, Valor, DataContext);
|
|
}
|
|
public void EliminaObjeto(object Registro)
|
|
{
|
|
if (this.Entry(Registro).State == Microsoft.EntityFrameworkCore.EntityState.Detached)
|
|
{
|
|
this.Entry(Registro).State = Microsoft.EntityFrameworkCore.EntityState.Unchanged;
|
|
}
|
|
else if (this.Entry(Registro).State == Microsoft.EntityFrameworkCore.EntityState.Added)
|
|
{
|
|
this.Remove(Registro);
|
|
this.Entry(Registro).State = Microsoft.EntityFrameworkCore.EntityState.Unchanged;
|
|
}
|
|
else
|
|
{
|
|
this.Remove(Registro);
|
|
}
|
|
|
|
}
|
|
|
|
public int GuardarCambios()
|
|
{
|
|
return this.SaveChanges();
|
|
}
|
|
|
|
public bool HayModificaciones()
|
|
{
|
|
return this.ChangeTracker.HasChanges();
|
|
}
|
|
public int ObtieneLongitudCampo(string NombreTablaBase, string NombreCampo)
|
|
{
|
|
return this.ObtieneMaximaLongitudCampo("bdEmtusa.db", NombreTablaBase, NombreCampo);
|
|
}
|
|
}
|
|
} |