OpenAuth.Net/OpenAuth.Repository/OpenAuthDBContext.cs

144 lines
6.5 KiB
C#
Raw Normal View History

2023-09-16 20:24:19 +08:00
using System;
using System.Linq;
using Infrastructure;
using Infrastructure.Extensions;
using Infrastructure.Utilities;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
2024-03-09 18:24:39 +08:00
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
2023-09-16 20:24:19 +08:00
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using OpenAuth.Repository.Domain;
using OpenAuth.Repository.QueryObj;
namespace OpenAuth.Repository
{
public partial class OpenAuthDBContext : DbContext
{
private ILoggerFactory _LoggerFactory;
private IHttpContextAccessor _httpContextAccessor;
private IConfiguration _configuration;
private IOptions<AppSetting> _appConfiguration;
public OpenAuthDBContext(DbContextOptions<OpenAuthDBContext> options, ILoggerFactory loggerFactory,
2024-04-09 22:12:22 +08:00
IHttpContextAccessor httpContextAccessor, IConfiguration configuration,
IOptions<AppSetting> appConfiguration)
2023-09-16 20:24:19 +08:00
: base(options)
{
_LoggerFactory = loggerFactory;
_httpContextAccessor = httpContextAccessor;
_configuration = configuration;
_appConfiguration = appConfiguration;
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
2024-04-09 22:12:22 +08:00
optionsBuilder.EnableSensitiveDataLogging(true); //允许打印参数
2023-09-16 20:24:19 +08:00
optionsBuilder.UseLoggerFactory(_LoggerFactory);
InitTenant(optionsBuilder);
base.OnConfiguring(optionsBuilder);
}
//初始化多租户信息根据租户id调整数据库
private void InitTenant(DbContextOptionsBuilder optionsBuilder)
{
var tenantId = _httpContextAccessor.GetTenantId();
string connect = _configuration.GetConnectionString(tenantId);
2024-04-09 22:12:22 +08:00
if (string.IsNullOrEmpty(connect))
2023-09-16 20:24:19 +08:00
{
throw new Exception($"未能找到租户{tenantId}对应的连接字符串信息");
}
//这个地方如果用IOption在单元测试的时候会获取不到AppSetting的值😅
var dbtypes = _configuration.GetSection("AppSetting:DbTypes").GetChildren()
.ToDictionary(x => x.Key, x => x.Value);
var dbType = dbtypes[tenantId];
2024-04-09 22:12:22 +08:00
if (dbType == Define.DBTYPE_SQLSERVER)
2023-09-16 20:24:19 +08:00
{
optionsBuilder.UseSqlServer(connect);
}
2024-04-09 22:12:22 +08:00
else if (dbType == Define.DBTYPE_MYSQL) //mysql
2023-09-16 20:24:19 +08:00
{
optionsBuilder.UseMySql(connect, new MySqlServerVersion(new Version(8, 0, 11)));
}
2024-04-09 22:12:22 +08:00
else if (dbType == Define.DBTYPE_PostgreSQL) //PostgreSQL
2023-09-16 20:24:19 +08:00
{
optionsBuilder.UseNpgsql(connect);
}
else
{
optionsBuilder.UseOracle(connect, options => options.UseOracleSQLCompatibility("11"));
}
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<DataPrivilegeRule>()
2024-04-09 22:12:22 +08:00
.HasKey(c => new {c.Id});
2023-09-16 20:24:19 +08:00
modelBuilder.Entity<SysTableColumn>().HasNoKey();
modelBuilder.Entity<QueryStringObj>().HasNoKey();
2024-04-09 22:12:22 +08:00
2024-03-09 18:24:39 +08:00
//converting between PostgreSQL smallint and .NET Boolean types
2024-04-09 22:12:22 +08:00
if (Database.ProviderName == "Npgsql.EntityFrameworkCore.PostgreSQL"
|| Database.ProviderName == "Oracle.EntityFrameworkCore")
2024-03-09 18:24:39 +08:00
{
var boolToSmallIntConverter = new ValueConverter<bool, short>(
2024-04-09 22:12:22 +08:00
v => v ? (short) 1 : (short) 0,
2024-03-09 18:24:39 +08:00
v => v != 0);
foreach (var entityType in modelBuilder.Model.GetEntityTypes())
{
foreach (var property in entityType.GetProperties())
{
if (property.ClrType == typeof(bool))
{
property.SetValueConverter(boolToSmallIntConverter);
}
2024-09-18 21:23:29 +08:00
//解决PostgreSQL时间戳问题
if (Database.ProviderName == "Npgsql.EntityFrameworkCore.PostgreSQL" &&
property.ClrType == typeof(DateTime))
{
property.SetValueConverter(new ValueConverter<DateTime, DateTime>(
v => v.ToUniversalTime(), v => DateTime.SpecifyKind(v, DateTimeKind.Utc)));
}
2024-03-09 18:24:39 +08:00
}
}
}
2023-09-16 20:24:19 +08:00
}
public virtual DbSet<Application> Applications { get; set; }
public virtual DbSet<Category> Categories { get; set; }
public virtual DbSet<CategoryType> CategoryTypes { get; set; }
public virtual DbSet<FlowInstance> FlowInstances { get; set; }
public virtual DbSet<FlowInstanceOperationHistory> FlowInstanceOperationHistorys { get; set; }
public virtual DbSet<FlowInstanceTransitionHistory> FlowInstanceTransitionHistorys { get; set; }
public virtual DbSet<FlowScheme> FlowSchemes { get; set; }
public virtual DbSet<Form> Forms { get; set; }
public virtual DbSet<Module> Modules { get; set; }
public virtual DbSet<ModuleElement> ModuleElements { get; set; }
public virtual DbSet<SysOrg> Orgs { get; set; }
public virtual DbSet<Relevance> Relevances { get; set; }
public virtual DbSet<Resource> Resources { get; set; }
public virtual DbSet<Role> Roles { get; set; }
public virtual DbSet<User> Users { get; set; }
public virtual DbSet<UploadFile> UploadFiles { get; set; }
public virtual DbSet<SysPrinterPlan> SysPrinterPlans { get; set; }
public virtual DbSet<FrmLeaveReq> FrmLeaveReqs { get; set; }
public virtual DbSet<SysLog> SysLogs { get; set; }
public virtual DbSet<SysMessage> SysMessages { get; set; }
public virtual DbSet<DataPrivilegeRule> DataPrivilegeRules { get; set; }
public virtual DbSet<WmsInboundOrderDtbl> WmsInboundOrderDtbls { get; set; }
public virtual DbSet<WmsInboundOrderTbl> WmsInboundOrderTbls { get; set; }
public virtual DbSet<OpenJob> OpenJobs { get; set; }
public virtual DbSet<BuilderTable> BuilderTables { get; set; }
2024-04-09 22:12:22 +08:00
2023-09-16 20:24:19 +08:00
public virtual DbSet<BuilderTableColumn> BuilderTableColumns { get; set; }
2024-04-09 22:12:22 +08:00
2023-09-16 20:24:19 +08:00
//非数据库表格
public virtual DbSet<QueryStringObj> QueryStringObjs { get; set; }
public virtual DbSet<SysTableColumn> SysTableColumns { get; set; }
2024-04-09 22:12:22 +08:00
}
}