133 lines
4.0 KiB
C#
133 lines
4.0 KiB
C#
using System;
|
|
using LiMan.DbSync.DbModels;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.EntityFrameworkCore.Metadata;
|
|
using Microsoft.Extensions.Configuration;
|
|
using NLog;
|
|
|
|
#nullable disable
|
|
|
|
namespace LiMan.DbSync
|
|
{
|
|
public partial class LMDbSyncContext : Microsoft.EntityFrameworkCore.DbContext
|
|
{
|
|
#region Private Fields
|
|
|
|
private static Logger Log = LogManager.GetCurrentClassLogger();
|
|
|
|
private IConfiguration _configuration;
|
|
|
|
#endregion Private Fields
|
|
|
|
#region Public Constructors
|
|
|
|
[Obsolete("This constructor should never be used directly, and is only needed to generate entityframework stuff. Connection string can be adapted as pleased.")]
|
|
public LMDbSyncContext()
|
|
{
|
|
}
|
|
|
|
public LMDbSyncContext(string cString)
|
|
{
|
|
connString = cString;
|
|
}
|
|
|
|
public LMDbSyncContext(IConfiguration configuration)
|
|
{
|
|
_configuration = configuration;
|
|
}
|
|
|
|
public LMDbSyncContext(DbContextOptions<LMDbSyncContext> options) : base(options)
|
|
{
|
|
try
|
|
{
|
|
// se non ci fosse... crea o migra!
|
|
Database.Migrate();
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error(exc, "Exception during context initialization 02");
|
|
}
|
|
}
|
|
|
|
#endregion Public Constructors
|
|
|
|
#region Public Properties
|
|
|
|
public virtual DbSet<AnagKeyValueModel> DbSetAnagKeyVal { get; set; }
|
|
public virtual DbSet<ConfigModel> DbSetConfig { get; set; }
|
|
public virtual DbSet<VocabolarioModel> DbSetVocabolario { get; set; }
|
|
|
|
#endregion Public Properties
|
|
|
|
partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
|
|
|
|
private string connString = "";
|
|
|
|
#region Protected Methods
|
|
|
|
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
|
{
|
|
if (!optionsBuilder.IsConfigured)
|
|
{
|
|
// se vuota rileggo da json
|
|
if (string.IsNullOrEmpty(connString))
|
|
{
|
|
connString = _configuration.GetConnectionString("LiMan.DB");
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(connString))
|
|
{
|
|
optionsBuilder.UseSqlServer(connString);
|
|
}
|
|
else
|
|
{
|
|
//optionsBuilder.UseSqlServer("Server=SQLSTEAM;Database=LiMan.DB;Trusted_Connection=True;");
|
|
optionsBuilder.UseSqlServer("Server=W2019-SQL-STEAM;Database=LiMan.DB;Trusted_Connection=True;");
|
|
//optionsBuilder.UseSqlServer("Server=W2019-SQL-STEAM;Database=LiMan.DB;User ID=sa;Password=keyhammer16;integrated security=False;MultipleActiveResultSets=True;App=LiMan.UI");
|
|
}
|
|
}
|
|
}
|
|
|
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
|
{
|
|
modelBuilder.HasAnnotation("Relational:Collation", "SQL_Latin1_General_CP1_CI_AS");
|
|
|
|
modelBuilder.Entity<VocabolarioModel>(entity =>
|
|
{
|
|
entity.HasKey(e => new { e.Lingua, e.Lemma });
|
|
|
|
entity.ToTable("Vocabolario");
|
|
|
|
entity.Property(e => e.Lingua).HasMaxLength(3);
|
|
|
|
entity.Property(e => e.Lemma).HasMaxLength(50);
|
|
|
|
entity.Property(e => e.Traduzione)
|
|
.IsRequired()
|
|
.HasMaxLength(500);
|
|
});
|
|
|
|
OnModelCreatingPartial(modelBuilder);
|
|
}
|
|
|
|
#endregion Protected Methods
|
|
|
|
#region Public Methods
|
|
|
|
public void DbForceMigrate()
|
|
{
|
|
try
|
|
{
|
|
// se non ci fosse... crea o migra!
|
|
Database.Migrate();
|
|
Log.Info("DbForceMigrate: done!");
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error(exc, "DbForceMigrate: Exception during context initialization 01");
|
|
}
|
|
}
|
|
|
|
#endregion Public Methods
|
|
}
|
|
} |