108 lines
4.1 KiB
C#
108 lines
4.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace CloudGaming.Repository.Phone;
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// 程序启动器
|
|
/// </summary>
|
|
[ImportStartupModule<CoreEntityFrameworkStartup>]
|
|
public class PhoneRepositoryStartup : StartupModule<PhoneRepositoryStartup>
|
|
{
|
|
/// <summary>
|
|
/// 程序启动器
|
|
/// </summary>
|
|
/// <param name="webApplicationBuilder"></param>
|
|
public override void ConfigureServices(WebApplicationBuilder webApplicationBuilder)
|
|
{
|
|
|
|
var configuration = webApplicationBuilder.Configuration;
|
|
var services = webApplicationBuilder.Services;
|
|
var webHostEnvironment = webApplicationBuilder.Environment;
|
|
var repositoryOptions = new RepositoryOptions()
|
|
{
|
|
ConnectionString = "Server=192.168.1.17;Database=CloudGamingPhone;User Id=sa;Password=Dbt@com@123;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=True;",
|
|
IsMonitorEFCore = true,
|
|
DefaultDatabaseType = DefaultDatabaseType.SqlServer
|
|
|
|
};
|
|
//var repositoryOptions = configuration
|
|
// .GetSection(nameof(AdminRepositoryOptions))
|
|
// .Get<AdminRepositoryOptions>() ?? throw new Exception("配置对象 空 异常!");
|
|
|
|
if (repositoryOptions.DefaultDatabaseType == DefaultDatabaseType.PostgreSql)
|
|
{
|
|
//EnableLegacyTimestampBehavior 启动旧行为,避免时区问题,存储时间报错
|
|
AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);
|
|
}
|
|
|
|
services
|
|
.AddDbContextFactory<PhoneDbContext>()
|
|
.AddEntityFrameworkRepositories<PhoneDbContext>(repositoryOptions, (auditOptions) =>
|
|
{
|
|
// 你的自定义审计字段 ...
|
|
//auditOptions.Add(new AuditOptions()
|
|
//{
|
|
// CreationTimeFieldName = nameof(ICreateEntityV2.CreateTime),
|
|
// CreatorUserIdFieldName = "",
|
|
// LastModificationTimeFieldName = nameof(IUpdateEntityV2.UpdateTime),
|
|
// LastModifierUserIdFieldName = "",
|
|
// DeletionTimeFieldName = "UpdateTime",
|
|
// DeleterUserIdFieldName = "UpdateBy",
|
|
// IsDeletedFieldName = "DelFlag",
|
|
//});
|
|
}, (freesqlOptions) =>
|
|
{
|
|
freesqlOptions.FreeSqlAuditAopList?.Add(new FreeSqlAuditAop());
|
|
freesqlOptions.FreeSqlAction = (freeSql) =>
|
|
{
|
|
freeSql.Aop.CurdAfter += (object? sender, FreeSql.Aop.CurdAfterEventArgs curdAfter) =>
|
|
{
|
|
//var stringBuilder = new StringBuilder();
|
|
//stringBuilder.Append($"\r\n====[FreeSql 开始 耗时: {curdAfter.ElapsedMilliseconds} ms]=========");
|
|
//stringBuilder.Append($"\r\n{curdAfter.Sql}");
|
|
//stringBuilder.Append($"\r\n====[FreeSql 结束 线程Id:{Environment.CurrentManagedThreadId}]=========");
|
|
//LogUtil.Log.Warning(stringBuilder.ToString());
|
|
//Console.WriteLine(stringBuilder.ToString());
|
|
};
|
|
};
|
|
});
|
|
}
|
|
|
|
/// <summary>
|
|
/// Configure
|
|
/// </summary>
|
|
/// <param name="webApplication"></param>
|
|
public override void Configure(WebApplication webApplication)
|
|
{
|
|
// 使用 DbContext
|
|
#region 开发环境检测是否需要数据库迁移
|
|
|
|
//if (webApplication.Environment.IsDevelopment())
|
|
//{
|
|
// // 自动迁移 (如果迁移文件有变动)
|
|
//using var scope = webApplication.Services.CreateScope();
|
|
//using var adminDbContext = scope.ServiceProvider.GetService<AdminDbContext>();
|
|
//if (adminDbContext!.Database.GetPendingMigrations().Count() > 0)
|
|
//{
|
|
// try
|
|
// {
|
|
// adminDbContext.Database.Migrate();
|
|
// }
|
|
// catch (Exception ex)
|
|
// {
|
|
// //LogUtil.Log.Error(ex.Message, ex);
|
|
// }
|
|
//}
|
|
#endregion
|
|
}
|
|
|
|
|
|
|
|
}
|