112 lines
3.6 KiB
C#
112 lines
3.6 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Configuration;
|
|
using NLog;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using WebDoorCreator.Data.DbModels;
|
|
using static Org.BouncyCastle.Math.EC.ECCurve;
|
|
|
|
namespace WebDoorCreator.Data
|
|
{
|
|
public partial class WDCDataContext : DbContext
|
|
{
|
|
private static NLog.Logger Log = LogManager.GetCurrentClassLogger();
|
|
|
|
private IConfiguration _configuration;
|
|
public WDCDataContext()
|
|
{
|
|
}
|
|
|
|
public WDCDataContext(IConfiguration configuration)
|
|
{
|
|
_configuration = configuration;
|
|
try
|
|
{
|
|
// se non ci fosse... crea o migra!
|
|
Database.Migrate();
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error(exc, "Exception during context initialization 02");
|
|
}
|
|
}
|
|
|
|
public WDCDataContext(DbContextOptions<WDCDataContext> options) : base(options)
|
|
{
|
|
try
|
|
{
|
|
// se non ci fosse... crea o migra!
|
|
Database.Migrate();
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error(exc, "Exception during context initialization 02");
|
|
}
|
|
}
|
|
|
|
|
|
public virtual DbSet<CompanyModel> DbSetCompany { get; set; }
|
|
public virtual DbSet<UsersModel> DbSetUsers { get; set; }
|
|
public virtual DbSet<RolesModel> DbSetRoles { get; set; }
|
|
public virtual DbSet<UserRolesModel> DbSetUserRoles { get; set; }
|
|
|
|
public virtual DbSet<OrderModel> DbSetOrders { get; set; }
|
|
public virtual DbSet<DoorModel> DbSetDoor { get; set; }
|
|
public virtual DbSet<DoorTypeModel> DbSetDoorType { get; set; }
|
|
|
|
|
|
|
|
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
|
{
|
|
if (!optionsBuilder.IsConfigured)
|
|
{
|
|
string connString = _configuration.GetConnectionString("WDC.DB");
|
|
if (!string.IsNullOrEmpty(connString))
|
|
{
|
|
optionsBuilder.UseSqlServer(connString);
|
|
}
|
|
else
|
|
{
|
|
optionsBuilder.UseSqlServer("Server=SQL2016DEV;Database=WebDoorCreator;Trusted_Connection=True;");
|
|
}
|
|
}
|
|
}
|
|
|
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
|
{
|
|
modelBuilder.UseCollation("Latin1_General_CI_AS");
|
|
|
|
//Excluding user tables since they are already generated by identity
|
|
modelBuilder.Entity<UsersModel>().ToTable(nameof(UsersModel), t => t.ExcludeFromMigrations());
|
|
modelBuilder.Entity<RolesModel>().ToTable(nameof(RolesModel), t => t.ExcludeFromMigrations());
|
|
modelBuilder.Entity<UserRolesModel>().ToTable(nameof(UserRolesModel), t => t.ExcludeFromMigrations());
|
|
|
|
|
|
modelBuilder.Entity<UserRolesModel>().HasKey(c => new { c.UserId, c.RoleId});
|
|
|
|
modelBuilder.ApplyConfiguration(new RoleConfiguration());
|
|
modelBuilder.Seed();
|
|
|
|
OnModelCreatingPartial(modelBuilder);
|
|
}
|
|
partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
|
|
|
|
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");
|
|
}
|
|
}
|
|
}
|
|
}
|