179 lines
5.5 KiB
C#
179 lines
5.5 KiB
C#
using System;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.EntityFrameworkCore.Metadata;
|
|
using Microsoft.Extensions.Configuration;
|
|
using MP.TaskMan.Models;
|
|
using NLog;
|
|
|
|
#nullable disable
|
|
// <Auto-Generated>
|
|
// This is here so CodeMaid doesn't reorganize this document
|
|
// </Auto-Generated>
|
|
namespace MP.TaskMan
|
|
{
|
|
public partial class TaskContext : DbContext
|
|
{
|
|
#region Private Fields
|
|
|
|
private static NLog.Logger Log = LogManager.GetCurrentClassLogger();
|
|
|
|
private IConfiguration _configuration;
|
|
|
|
#endregion Private Fields
|
|
|
|
#region Public Constructors
|
|
|
|
private bool DesignTime = false;
|
|
|
|
/// <summary>
|
|
/// Indispensabile x generazione migrations EFCore
|
|
/// </summary>
|
|
[Obsolete("This constructor should never be used directly, and is only needed to generate entityframework stuff. Connection string can be adapted as pleased.", false)]
|
|
public TaskContext()
|
|
{
|
|
#if DEBUG
|
|
DesignTime = true;
|
|
try
|
|
{
|
|
// se non ci fosse... crea o migra!
|
|
Database.Migrate();
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Exception during context initialization 01.A{Environment.NewLine}{exc}");
|
|
}
|
|
|
|
#endif
|
|
}
|
|
|
|
public TaskContext(IConfiguration configuration)
|
|
{
|
|
_configuration = configuration;
|
|
try
|
|
{
|
|
// se non ci fosse... crea o migra!
|
|
Database.Migrate();
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Exception during context initialization 01.B{Environment.NewLine}{exc}");
|
|
}
|
|
|
|
}
|
|
|
|
public TaskContext(DbContextOptions<TaskContext> options) : base(options)
|
|
{
|
|
try
|
|
{
|
|
// se non ci fosse... crea o migra!
|
|
Database.Migrate();
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Exception during context initialization 01.C{Environment.NewLine}{exc}");
|
|
}
|
|
|
|
}
|
|
|
|
#endregion Public Constructors
|
|
|
|
#region Public Properties
|
|
|
|
|
|
public virtual DbSet<TaskListModel> DbSetTaskList { get; set; }
|
|
public virtual DbSet<TaskExecModel> DbSetTaskExe { get; set; }
|
|
public virtual DbSet<TaskResultModel> DbSetTaskResult { get; set; } = null!;
|
|
|
|
|
|
#endregion Public Properties
|
|
|
|
#region Private Methods
|
|
|
|
partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
|
|
|
|
#endregion Private Methods
|
|
|
|
#region Protected Methods
|
|
|
|
/// <summary>
|
|
/// Stringa di connessione impiegata
|
|
/// </summary>
|
|
private string connString { get; set; } = "";
|
|
|
|
/// <summary>
|
|
/// Ovewrride setup gestione stringa di connessione
|
|
/// </summary>
|
|
/// <param name="optionsBuilder"></param>
|
|
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
|
{
|
|
if (!optionsBuilder.IsConfigured)
|
|
{
|
|
if (DesignTime)
|
|
{
|
|
connString = "Server=SQL2016DEV; Database=MoonPro_PROG; Trusted_Connection=True;";
|
|
}
|
|
else
|
|
{
|
|
// recupero la connString tra i candidati...
|
|
connString = ConnStringGetFirst();
|
|
}
|
|
// avvio con stringa connessione trovata
|
|
optionsBuilder.UseSqlServer(connString);
|
|
//optionsBuilder.UseSqlServer("Server=SQL2016DEV; Database=MoonPro_PROG; Trusted_Connection=True;");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Cerca la connString del DB usato da stats basandosi sull'elenco dei nomi delle chaivi da cercare, dalle + specifiche alle più generiche
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
private string ConnStringGetFirst()
|
|
{
|
|
// in primis cerco se c'è la conf di quale connString usare per il programma specifico
|
|
string scTaskConn = _configuration.GetValue<string>("SpecialConf:TaskManConn");
|
|
string connString = _configuration.GetConnectionString(scTaskConn);
|
|
|
|
// altrimenti ciclo tra le conf alternative standard la + specifica
|
|
if (string.IsNullOrEmpty(connString))
|
|
{
|
|
foreach (var keyName in ConnStringList)
|
|
{
|
|
connString = _configuration.GetConnectionString(keyName);
|
|
if (!string.IsNullOrEmpty(connString))
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
return connString;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Elenco dei nomi delle connString da provare x il DB di riferimento qualora non specificato in conf
|
|
/// </summary>
|
|
protected List<string> ConnStringList { get; set; } = new List<string>()
|
|
{
|
|
"MP.TaskMan",
|
|
"MP.STATS",
|
|
"MP.SPEC",
|
|
"MP.Land",
|
|
"MP.Mon",
|
|
"MP.Data",
|
|
"MP.All"
|
|
};
|
|
|
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
|
{
|
|
modelBuilder.HasAnnotation("Relational:Collation", "SQL_Latin1_General_CP1_CI_AS");
|
|
|
|
modelBuilder.Entity<TaskResultModel>(entity =>
|
|
{
|
|
entity.HasKey(e => e.Task);
|
|
});
|
|
|
|
OnModelCreatingPartial(modelBuilder);
|
|
}
|
|
|
|
#endregion Protected Methods
|
|
}
|
|
} |