diff --git a/DAL/WebApiContext.cs b/DAL/WebApiContext.cs index 487836c..60231f0 100644 --- a/DAL/WebApiContext.cs +++ b/DAL/WebApiContext.cs @@ -1,19 +1,24 @@ using DAL._Shared.SharedModels; using DAL.Models.Entities; using HRD.WebApi.DAL; - using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Metadata; -using System.Collections; -using System.Collections.Generic; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; using System.Linq; namespace DAL { public partial class WebApiContext : WebApiBaseContext { - public WebApiContext(DbContextOptions options) : base(options) + private readonly WebApiContextOptions _options; + + private readonly ILogger _logger; + + public WebApiContext(DbContextOptions options, IOptions webApiContextOptions, ILogger logger) : base(options) { + _options = webApiContextOptions.Value; + _logger = logger; } public virtual DbSet WebAppUserSet { get; set; } @@ -48,7 +53,333 @@ namespace DAL public virtual DbSet WindreamSearchToDepartmentSet { get; set; } public virtual DbSet WindreamInputFolderSet { get; set; } public virtual DbSet SubsidiarySet { get; set; } + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + switch(_options.TableNamingRule) + { + case "DIGITAL_DATA": + OnModelCreating_DDSQLNaming(modelBuilder); + break; + case "PREPARED-SQL": + OnModelCreating_PreparedSQL(modelBuilder); + break; + default: + _logger.LogWarning($"The WebApiContextOptions.TableNamingRule is set to {_options?.TableNamingRule ?? "NULL"} in appsettings.json. Hence, it has been defaulted to PREPARED-SQL."); + OnModelCreating_PreparedSQL(modelBuilder); + break; + } + + base.OnModelCreating(modelBuilder); + } + + protected static void OnModelCreating_PreparedSQL(ModelBuilder modelBuilder) + { + modelBuilder.Entity(entity => + { + entity.ToTable("Subsidiary", "dbo"); + }); + + modelBuilder.Entity(entity => + { + entity.ToTable("WindreamInputFolder", "ecm"); + }); + + modelBuilder.Entity(entity => + { + entity.ToTable("WindreamSearchToDepartment", "ecm"); + }); + + modelBuilder.Entity(entity => + { + entity.ToTable("WindreamSearchItemToWindreamSearchToDepartment", "ecm"); + }); + + modelBuilder.Entity(entity => + { + entity.ToTable("WindreamSearchItem", "ecm"); + }); + + modelBuilder.Entity(entity => + { + entity.ToTable("WindreamSearch", "ecm"); + }); + + modelBuilder.Entity(entity => + { + entity.ToTable("WindreamIndexToWindreamSearchToDepartment", "ecm"); + }); + + modelBuilder.Entity(entity => + { + entity.ToTable("WindreamIndex", "ecm"); + }); + + // 'WindreamColumnsToDepartment' tablosu güncellenmiş listeye dahil değil + + modelBuilder.Entity(entity => + { + entity.ToTable("WebAppToWebAppRole", "dbo"); + }); + + modelBuilder.Entity(entity => + { + entity.ToTable("WebAppToWebAppAdditionalRole", "dbo"); + }); + + modelBuilder.Entity(entity => + { + entity.ToTable("WebAppToDepartment", "dbo"); + }); + + modelBuilder.Entity(entity => + { + entity.ToTable("WebAppAdditionalRole", "dbo"); + }); + + modelBuilder.Entity(entity => + { + entity.ToTable("EmployeeToWebApp", "hr"); + }); + + modelBuilder.Entity(entity => + { + entity.ToTable("EmployeeToDepartment", "hr"); + }); + + modelBuilder.Entity(entity => + { + entity.ToTable("EmployeeToAttribute", "hr"); + }); + + modelBuilder.Entity(entity => + { + entity.ToTable("Employee", "hr"); + }); + + modelBuilder.Entity(entity => + { + entity.ToTable("DocumentArtToDepartment", "ecm"); + }); + + // 'DepartmentToWebAppToEmployeeForWindream' tablosu güncellenmiş listeye dahil değil + + modelBuilder.Entity(entity => + { + entity.ToTable("WebAppRole", "dbo"); + }); + + modelBuilder.Entity(entity => + { + entity.ToTable("WebApp", "dbo"); + }); + + modelBuilder.Entity(entity => + { + entity.ToTable("Rang", "hr"); + }); + + modelBuilder.Entity(entity => + { + entity.ToTable("Project", "hr"); + }); + + modelBuilder.Entity(entity => + { + entity.ToTable("EmployeeStatus", "hr"); + }); + + modelBuilder.Entity(entity => + { + entity.ToTable("EmployeeAttribute", "hr"); + }); + + modelBuilder.Entity(entity => + { + entity.ToTable("DocumentArt", "ecm"); + }); + + modelBuilder.Entity(entity => + { + entity.ToTable("Department", "hr"); + }); + + modelBuilder.Entity(entity => + { + entity.ToTable("CostCentre", "hr"); + }); + + // 'AdWebAppToWebAppRole' tablosu güncellenmiş listeye dahil değil + + modelBuilder.Entity(entity => + { + entity.ToTable("WebAppUser", "dbo"); + }); + + // 'WebAppEmployeeInfo' tablosu güncellenmiş listeye dahil değil + } + + protected static void OnModelCreating_ClassName(ModelBuilder modelBuilder) + { + modelBuilder.Entity(entity => + { + entity.ToTable("Subsidiary", "webapi"); + }); + + modelBuilder.Entity(entity => + { + entity.ToTable("WindreamInputFolder", "webapi"); + }); + + modelBuilder.Entity(entity => + { + entity.ToTable("WindreamSearchToDepartment", "webapi"); + }); + + modelBuilder.Entity(entity => + { + entity.ToTable("WindreamSearchItemToWindreamSearchToDepartment", "webapi"); + }); + + modelBuilder.Entity(entity => + { + entity.ToTable("WindreamSearchItem", "webapi"); + }); + + modelBuilder.Entity(entity => + { + entity.ToTable("WindreamSearch", "webapi"); + }); + + modelBuilder.Entity(entity => + { + entity.ToTable("WindreamIndexToWindreamSearchToDepartment", "webapi"); + }); + + modelBuilder.Entity(entity => + { + entity.ToTable("WindreamIndex", "webapi"); + }); + + modelBuilder.Entity(entity => + { + entity.ToTable("WindreamColumnsToDepartment", "webapi"); + }); + + modelBuilder.Entity(entity => + { + entity.ToTable("WebAppToWebAppRole", "webapi"); + }); + + modelBuilder.Entity(entity => + { + entity.ToTable("WebAppToWebAppAdditionalRole", "webapi"); + }); + + modelBuilder.Entity(entity => + { + entity.ToTable("WebAppToDepartment", "webapi"); + }); + + modelBuilder.Entity(entity => + { + entity.ToTable("WebAppAdditionalRole", "webapi"); + }); + + modelBuilder.Entity(entity => + { + entity.ToTable("EmployeeToWebApp", "webapi"); + }); + + modelBuilder.Entity(entity => + { + entity.ToTable("EmployeeToDepartment", "webapi"); + }); + + modelBuilder.Entity(entity => + { + entity.ToTable("EmployeeToAttribute", "webapi"); + }); + + modelBuilder.Entity(entity => + { + entity.ToTable("Employee", "webapi"); + }); + + modelBuilder.Entity(entity => + { + entity.ToTable("DocumentArtToDepartment", "webapi"); + }); + + modelBuilder.Entity(entity => + { + entity.ToTable("DepartmentToWebAppToEmployeeForWindream", "webapi"); + }); + + modelBuilder.Entity(entity => + { + entity.ToTable("WebAppRole", "webapi"); + }); + + modelBuilder.Entity(entity => + { + entity.ToTable("WebApp", "webapi"); + }); + + modelBuilder.Entity(entity => + { + entity.ToTable("Rang", "webapi"); + }); + + modelBuilder.Entity(entity => + { + entity.ToTable("Project", "webapi"); + }); + + modelBuilder.Entity(entity => + { + entity.ToTable("EmployeeStatus", "webapi"); + }); + + modelBuilder.Entity(entity => + { + entity.ToTable("EmployeeAttribute", "webapi"); + }); + + modelBuilder.Entity(entity => + { + entity.ToTable("DocumentArt", "webapi"); + }); + + modelBuilder.Entity(entity => + { + entity.ToTable("Department", "webapi"); + }); + + modelBuilder.Entity(entity => + { + entity.ToTable("CostCentre", "webapi"); + }); + + modelBuilder.Entity(entity => + { + entity.ToTable("AdWebAppToWebAppRole", "webapi"); + }); + + + modelBuilder.Entity(entity => + { + entity.ToTable("WebAppUser", "webapi"); + }); + + modelBuilder.Entity(entity => + { + entity.ToTable("WebAppEmployeeInfo", "webapi"); + }); + } + + #region MODEL CREATIN FOR DIGIDAL-DATA NAMING + protected static void OnModelCreating_DDSQLNaming(ModelBuilder modelBuilder) { modelBuilder.Entity(entity => { @@ -206,11 +537,9 @@ namespace DAL }); Configure4DDNaming(modelBuilder.Model.GetEntityTypes().ToArray()); - - base.OnModelCreating(modelBuilder); } - public static void Configure4DDNaming(params IMutableEntityType[] entityTypes) + private static void Configure4DDNaming(params IMutableEntityType[] entityTypes) { foreach (var entityType in entityTypes) { @@ -233,5 +562,6 @@ namespace DAL var startUnderscores = System.Text.RegularExpressions.Regex.Match(input, @"^_+"); return startUnderscores + System.Text.RegularExpressions.Regex.Replace(input, @"([a-z0-9])([A-Z])", "$1_$2").ToUpper(); } + #endregion } } diff --git a/DAL/WebApiContextOptions.cs b/DAL/WebApiContextOptions.cs new file mode 100644 index 0000000..19f9eba --- /dev/null +++ b/DAL/WebApiContextOptions.cs @@ -0,0 +1,9 @@ +namespace DAL +{ +#nullable enable + public class WebApiContextOptions + { + public string? TableNamingRule { get; init; } = null; + } +#nullable restore +} \ No newline at end of file diff --git a/StaffDBServer/Program.cs b/StaffDBServer/Program.cs index 28e1ac0..cfd2269 100644 --- a/StaffDBServer/Program.cs +++ b/StaffDBServer/Program.cs @@ -107,6 +107,8 @@ try var cnnStr = WebApiConfig.ConnectionString(EN_ConnectionType.SQLServer); + builder.Services.Configure(builder.Configuration.GetSection("WebApiContextOptions")); + builder.Services.AddDbContext(options => { const int dbTimeoutInMin = 5; diff --git a/StaffDBServer/appsettings.json b/StaffDBServer/appsettings.json index 0ed07d6..0ba4e24 100644 --- a/StaffDBServer/appsettings.json +++ b/StaffDBServer/appsettings.json @@ -5,7 +5,8 @@ } }, "ConnectionStrings": { - "sqlConnection": "Server=SDD-VMP04-SQL17\\DD_DEVELOP01;Database=DD_ECM;User Id=sa;Password=dd;Encrypt=false;TrustServerCertificate=True;" + "sqlConnection": "Server=SDD-VMP04-SQL17\\DD_DEVELOP01;Database=DD_ECM;User Id=sa;Password=dd;Encrypt=false;TrustServerCertificate=True;", + "sqlConnection19": "Server=SDD-VMP04-SQL19\\DD_DEVELOP01;Database=DD_StaffDB;User Id=sa;Password=dd;Encrypt=false;TrustServerCertificate=True;" }, "AppConfig": { "LDAP_WebAppGroup_Is_Live": "false", @@ -37,5 +38,11 @@ "LDAP_EDM_Prefix": "GG_EDM", "LDAP_WebAppp_Prefix": "GG_WebApp", "LDAP_Prefix_Test": "__Test" + }, + // else if created with prepared crate table SQL,TableNamingRule is 'PREPARED-SQL' (e.g. dbo.Subsidiary, ecm.WindreamIndexToWindreamSearchToDepartment) + // else if you follow digital data SQL table naming rules, TableNamingRule is 'DIGITAL_DATA' (e.g. TBSTF_WEB_APP_ROLE) + // otherwise, TableNamingRule is 'PREPARED-SQL' + "WebApiContextOptions": { + "TableNamingRule": "DIGITAL_DATA" } } \ No newline at end of file