99 lines
2.8 KiB
C#
99 lines
2.8 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Configuration;
|
|
using MP.AppAuth.Models;
|
|
using NLog;
|
|
using System;
|
|
|
|
namespace MP.AppAuth
|
|
{
|
|
public partial class UserAuthContext : DbContext
|
|
{
|
|
#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 UserAuthContext()
|
|
{
|
|
}
|
|
|
|
public UserAuthContext(IConfiguration configuration)
|
|
{
|
|
_configuration = configuration;
|
|
try
|
|
{
|
|
// se non ci fosse... crea o migra!
|
|
Database.Migrate();
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error(exc, "Exception during context initialization 01");
|
|
}
|
|
}
|
|
|
|
public UserAuthContext(DbContextOptions<AppAuthContext> 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<UserDirittiModel> DbSetUserDiritti { get; set; } = null!;
|
|
|
|
#endregion Public Properties
|
|
|
|
#region Protected Methods
|
|
|
|
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
|
{
|
|
if (!optionsBuilder.IsConfigured)
|
|
{
|
|
string connString = _configuration.GetConnectionString("MP.Land.Auth");
|
|
if (!string.IsNullOrEmpty(connString))
|
|
{
|
|
optionsBuilder.UseSqlServer(connString);
|
|
}
|
|
else
|
|
{
|
|
optionsBuilder.UseSqlServer("Server=SQL2016DEV;Database=MoonPro_Anagrafica;Trusted_Connection=True;");
|
|
}
|
|
}
|
|
}
|
|
|
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
|
{
|
|
modelBuilder.Entity<UserDirittiModel>(entity =>
|
|
{
|
|
entity.HasKey(e => new { e.UserName, e.CdC, e.Modulo, e.Funzione });
|
|
|
|
entity.ToTable("DIRITTI");
|
|
});
|
|
|
|
OnModelCreatingPartial(modelBuilder);
|
|
}
|
|
|
|
#endregion Protected Methods
|
|
|
|
#region Private Fields
|
|
|
|
private static NLog.Logger Log = LogManager.GetCurrentClassLogger();
|
|
|
|
private IConfiguration _configuration;
|
|
|
|
#endregion Private Fields
|
|
|
|
#region Private Methods
|
|
|
|
partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
|
|
|
|
#endregion Private Methods
|
|
}
|
|
} |