158 lines
5.8 KiB
C#
158 lines
5.8 KiB
C#
using HZY.Framework.Repository.EntityFramework.Attributes;
|
|
using HZY.Framework.Repository.EntityFramework.Models.Enums;
|
|
using HZY.Framework.Repository.EntityFramework.Models;
|
|
using HZY.Framework.Repository.EntityFramework.Repositories.Impl;
|
|
using HZY.Framework.Repository.EntityFramework;
|
|
using Microsoft.AspNetCore.Hosting;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using HZY.Framework.Repository.EntityFramework.Repositories;
|
|
using FreeSql.DatabaseModel;
|
|
|
|
namespace CloudGaming.Code.DataBaseModel;
|
|
|
|
public class BaseDbContext : DbContext, IBaseDbContext
|
|
{
|
|
/// <summary>
|
|
/// 工作单元
|
|
/// </summary>
|
|
public virtual IUnitOfWork UnitOfWork { get; }
|
|
|
|
private readonly AppConfig _appConfig;
|
|
private readonly AppDataBaseType _appDataBaseType;
|
|
private readonly IConfiguration _configuration;
|
|
private readonly IWebHostEnvironment _webHostEnvironment;
|
|
|
|
public BaseDbContext(IConfiguration configuration, IWebHostEnvironment webHostEnvironment, AppConfig appConfig, AppDataBaseType appDataBaseType)
|
|
{
|
|
_configuration = configuration;
|
|
_webHostEnvironment = webHostEnvironment;
|
|
_appConfig = appConfig;
|
|
if (_appConfig == null)
|
|
{
|
|
_appConfig = new AppConfig
|
|
{
|
|
ExtConnectionString = "Server=192.168.1.17;Database=CloudGamingCBT;User Id=sa;Password=Dbt@com@123;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=True;",
|
|
GameConnectionString = "Server=192.168.1.17;Database=CloudGamingGame;User Id=sa;Password=Dbt@com@123;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=True;",
|
|
PhoneConnectionString = "Server=192.168.1.17;Database=CloudGamingPhone;User Id=sa;Password=Dbt@com@123;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=True;",
|
|
UserConnectionString = "Server=192.168.1.17;Database=CloudGamingUser;User Id=sa;Password=Dbt@com@123;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=True;",
|
|
};
|
|
}
|
|
_appDataBaseType = appDataBaseType;
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取仓储配置
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
/// <exception cref="Exception"></exception>
|
|
public RepositoryOptions GetRepositoryOptions()
|
|
{
|
|
var repositoryOptions = new RepositoryOptions()
|
|
{
|
|
ConnectionString = _appConfig.GetConnectionString(_appDataBaseType),
|
|
IsMonitorEFCore = true,
|
|
DefaultDatabaseType = DefaultDatabaseType.SqlServer
|
|
|
|
};
|
|
|
|
return repositoryOptions;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 配置
|
|
/// </summary>
|
|
/// <param name="optionsBuilder"></param>
|
|
/// <exception cref="Exception"></exception>
|
|
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
|
{
|
|
var repositoryOptions = this.GetRepositoryOptions();
|
|
|
|
// 获取连接字符串
|
|
var connectionString = repositoryOptions.ConnectionString;
|
|
connectionString = string.IsNullOrWhiteSpace(connectionString) ?
|
|
_configuration["ConnectionStrings:" + repositoryOptions!.DefaultDatabaseType.ToString()] :
|
|
connectionString;
|
|
|
|
switch (repositoryOptions.DefaultDatabaseType)
|
|
{
|
|
case DefaultDatabaseType.SqlServer:
|
|
optionsBuilder
|
|
.UseSqlServer(connectionString, w => w.MinBatchSize(1).MaxBatchSize(1000))
|
|
;
|
|
break;
|
|
case DefaultDatabaseType.MySql:
|
|
optionsBuilder
|
|
.UseMySql(connectionString, MySqlServerVersion.LatestSupportedServerVersion, w => w.MinBatchSize(1).MaxBatchSize(1000))
|
|
;
|
|
break;
|
|
case DefaultDatabaseType.PostgreSql:
|
|
optionsBuilder
|
|
.UseNpgsql(connectionString, w => w.MinBatchSize(1).MaxBatchSize(1000))
|
|
;
|
|
break;
|
|
case DefaultDatabaseType.Oracle:
|
|
optionsBuilder
|
|
.UseOracle(connectionString, w => w.MinBatchSize(1).MaxBatchSize(1000))
|
|
;
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
|
|
if (_webHostEnvironment.IsDevelopment())
|
|
{
|
|
// sql 日志写入控制台
|
|
var loggerFactory = LoggerFactory.Create(builder => builder.AddConsole(configure =>
|
|
{
|
|
configure.LogToStandardErrorThreshold = LogLevel.Error;
|
|
}));
|
|
optionsBuilder.UseLoggerFactory(loggerFactory)
|
|
//.EnableDetailedErrors()
|
|
//.EnableSensitiveDataLogging(true)
|
|
;
|
|
}
|
|
|
|
// 懒加载代理
|
|
//options.UseLazyLoadingProxies();
|
|
//添加 EFCore 监控 和 动态表名
|
|
optionsBuilder.AddEntityFrameworkMonitor(repositoryOptions.IsMonitorEFCore);
|
|
optionsBuilder.AddInterceptors(new AuditInterceptor());
|
|
}
|
|
|
|
/// <summary>
|
|
/// 模型创建
|
|
/// </summary>
|
|
/// <param name="modelBuilder"></param>
|
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
|
{
|
|
var dbContextConfigAttribute = GetType().GetCustomAttribute<DbContextConfigAttribute>()!;
|
|
dbContextConfigAttribute!.OnModelCreating(modelBuilder, dbContextConfigAttribute.GetModelTypes(GetType()));
|
|
|
|
#region 自动迁移种子数据
|
|
|
|
//ModelBuilderExtensions.Seed(modelBuilder);
|
|
|
|
#endregion
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public List<DbTableInfo> GetDbTableInfo()
|
|
{
|
|
var list = this.UnitOfWork.FreeSqlOrm.DbFirst.GetTablesByDatabase();
|
|
list.ForEach(item => item.Schema = _appDataBaseType.ToString());
|
|
return list;
|
|
}
|
|
} |