82 lines
2.8 KiB
C#
82 lines
2.8 KiB
C#
using CloudGaming.AppConfigModel;
|
|
|
|
using AppConfig = CloudGaming.Code.DataBaseModel.AppConfig;
|
|
|
|
using Microsoft.AspNetCore.Hosting;
|
|
|
|
namespace CloudGaming.Repository.Game;
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
[DbContextConfig($"Repository.*.Entities.Game.*")]
|
|
public class GameDbContext : BaseDbContext
|
|
{
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public override IUnitOfWork UnitOfWork { get; }
|
|
public GameDbContext(IConfiguration configuration, IWebHostEnvironment webHostEnvironment, AppConfig appConfig) : base(configuration, webHostEnvironment, appConfig, AppDataBaseType.Game)
|
|
{
|
|
UnitOfWork = new UnitOfWorkImpl<GameDbContext>(this);
|
|
}
|
|
/// <summary>
|
|
/// 配置
|
|
/// </summary>
|
|
/// <param name="optionsBuilder"></param>
|
|
/// <exception cref="Exception"></exception>
|
|
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
|
{
|
|
var repositoryOptions = this.GetRepositoryOptions();
|
|
|
|
// 获取连接字符串
|
|
var connectionString = repositoryOptions.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());
|
|
}
|
|
}
|