OpenAuth.Net/OpenAuth.Repository/OpenAuthDBContext.cs

125 lines
5.2 KiB
C#
Raw Normal View History

using System;
using System.Linq;
using Infrastructure;
using Infrastructure.Extensions;
using Infrastructure.Utilities;
2021-01-14 23:35:54 +08:00
using Microsoft.AspNetCore.Http;
2020-12-27 00:00:28 +08:00
using Microsoft.EntityFrameworkCore;
2021-01-14 23:35:54 +08:00
using Microsoft.Extensions.Configuration;
2020-12-27 00:00:28 +08:00
using Microsoft.Extensions.Logging;
2021-01-14 23:35:54 +08:00
using Microsoft.Extensions.Options;
2017-11-29 20:49:14 +08:00
using OpenAuth.Repository.Domain;
using OpenAuth.Repository.QueryObj;
2015-10-26 21:58:12 +08:00
namespace OpenAuth.Repository
2015-10-26 21:58:12 +08:00
{
public partial class OpenAuthDBContext : DbContext
2015-10-26 21:58:12 +08:00
{
2018-05-07 14:47:16 +08:00
2020-12-27 00:00:28 +08:00
private ILoggerFactory _LoggerFactory;
2021-01-14 23:35:54 +08:00
private IHttpContextAccessor _httpContextAccessor;
private IConfiguration _configuration;
private IOptions<AppSetting> _appConfiguration;
2020-12-27 00:00:28 +08:00
public OpenAuthDBContext(DbContextOptions<OpenAuthDBContext> options, ILoggerFactory loggerFactory,
IHttpContextAccessor httpContextAccessor, IConfiguration configuration, IOptions<AppSetting> appConfiguration)
: base(options)
2020-12-27 00:00:28 +08:00
{
_LoggerFactory = loggerFactory;
2021-01-14 23:35:54 +08:00
_httpContextAccessor = httpContextAccessor;
_configuration = configuration;
_appConfiguration = appConfiguration;
2021-01-14 23:35:54 +08:00
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.EnableSensitiveDataLogging(true); //允许打印参数
optionsBuilder.UseLoggerFactory(_LoggerFactory);
InitTenant(optionsBuilder);
base.OnConfiguring(optionsBuilder);
}
//初始化多租户信息根据租户id调整数据库
private void InitTenant(DbContextOptionsBuilder optionsBuilder)
{
var tenantId = _httpContextAccessor.GetTenantId();
2021-01-14 23:35:54 +08:00
string connect = _configuration.GetConnectionString(tenantId);
if(string.IsNullOrEmpty(connect))
{
throw new Exception($"未能找到租户{tenantId}对应的连接字符串信息");
}
2021-01-14 23:35:54 +08:00
//这个地方如果用IOption在单元测试的时候会获取不到AppSetting的值😅
var dbtypes = _configuration.GetSection("AppSetting:DbTypes").GetChildren()
.ToDictionary(x => x.Key, x => x.Value);
var dbType = dbtypes[tenantId];
if(dbType == Define.DBTYPE_SQLSERVER)
{
optionsBuilder.UseSqlServer(connect);
2021-01-14 23:35:54 +08:00
}
else if(dbType == Define.DBTYPE_MYSQL) //mysql
{
optionsBuilder.UseMySql(connect, new MySqlServerVersion(new Version(8, 0, 11)));
}
else if(dbType == Define.DBTYPE_PostgreSQL) //PostgreSQL
{
optionsBuilder.UseNpgsql(connect);
}
else
{
optionsBuilder.UseOracle(connect, options => options.UseOracleSQLCompatibility("11"));
}
2021-01-14 23:35:54 +08:00
2020-12-27 00:00:28 +08:00
}
2021-01-14 23:35:54 +08:00
protected override void OnModelCreating(ModelBuilder modelBuilder)
2018-05-07 14:47:16 +08:00
{
modelBuilder.Entity<DataPrivilegeRule>()
.HasKey(c => new { c.Id });
modelBuilder.Entity<SysTableColumn>().HasNoKey();
modelBuilder.Entity<QueryStringObj>().HasNoKey();
2018-05-07 14:47:16 +08:00
}
2015-10-26 21:58:12 +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<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; }
public virtual DbSet<BuilderTableColumn> BuilderTableColumns { get; set; }
//非数据库表格
public virtual DbSet<QueryStringObj> QueryStringObjs { get; set; }
public virtual DbSet<SysTableColumn> SysTableColumns { get; set; }
2018-02-28 17:34:27 +08:00
2015-10-26 21:58:12 +08:00
}
}