219 lines
8.8 KiB
C#
219 lines
8.8 KiB
C#
using CloudGaming.AppConfigModel;
|
|
using CloudGaming.Code.DataBaseModel;
|
|
|
|
|
|
using FreeSql.DatabaseModel;
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
using AppConfig = CloudGaming.Code.DataBaseModel.AppConfig;
|
|
|
|
namespace CloudGaming.Repository.Game;
|
|
|
|
/// <summary>
|
|
/// 程序启动器
|
|
/// </summary>
|
|
[ImportStartupModule<CoreEntityFrameworkStartup>]
|
|
|
|
public class GameRepositoryStartup : StartupModule<GameRepositoryStartup>
|
|
{
|
|
public override void ConfigureServices(WebApplicationBuilder webApplicationBuilder)
|
|
{
|
|
var configuration = webApplicationBuilder.Configuration;
|
|
var services = webApplicationBuilder.Services;
|
|
var webHostEnvironment = webApplicationBuilder.Environment;
|
|
//configuration.
|
|
var tenants = configuration.GetSection("Tenants").Get<List<AppConfig>>();
|
|
var _appConfig = new AppConfig
|
|
{
|
|
ExtConnectionString = "Server=1.15.21.245;Database=CloudGamingCBT;User Id=sa;Password=Dbt@com@123;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=True;",
|
|
GameConnectionString = "Server=1.15.21.245;Database=CloudGamingGame;User Id=sa;Password=Dbt@com@123;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=True;",
|
|
PhoneConnectionString = "Server=1.15.21.245;Database=CloudGamingPhone;User Id=sa;Password=Dbt@com@123;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=True;",
|
|
UserConnectionString = "Server=1.15.21.245;Database=CloudGamingUser;User Id=sa;Password=Dbt@com@123;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=True;",
|
|
};
|
|
if (tenants != null && tenants.Count > 0)
|
|
{
|
|
_appConfig = tenants[0];
|
|
}
|
|
|
|
|
|
AddDbContextAndRepositories<GameDbContext>(services, _appConfig, AppDataBaseType.Game);
|
|
AddDbContextAndRepositories<ExtDbContext>(services, _appConfig, AppDataBaseType.Ext);
|
|
AddDbContextAndRepositories<PhoneDbContext>(services, _appConfig, AppDataBaseType.App);
|
|
AddDbContextAndRepositories<UserDbContext>(services, _appConfig, AppDataBaseType.User);
|
|
|
|
|
|
//services
|
|
// .AddDbContextFactory<ExtDbContext>()
|
|
// .AddEntityFrameworkRepositories<ExtDbContext>(_appConfig.ToRepositoryOptions(AppDataBaseType.Ext), (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());
|
|
// };
|
|
// };
|
|
// });
|
|
|
|
}
|
|
private void AddDbContextAndRepositories<TContext>(IServiceCollection services, AppConfig _appConfig, AppDataBaseType databaseType) where TContext : DbContext
|
|
{
|
|
var repositoryOptions = _appConfig.ToRepositoryOptions(databaseType);
|
|
services
|
|
.AddDbContextFactory<TContext>()
|
|
.AddEntityFrameworkRepositories<TContext>(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 CloudGamingAuditAop());
|
|
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
|
|
}
|
|
}
|
|
public static class GameRepositoryExtend
|
|
{
|
|
public static RepositoryOptions ToRepositoryOptions(this AppConfig appConfig, AppDataBaseType appDataBaseType)
|
|
{
|
|
return new RepositoryOptions
|
|
{
|
|
ConnectionString = appConfig.GetConnectionString(appDataBaseType),
|
|
IsMonitorEFCore = true,
|
|
DefaultDatabaseType = DefaultDatabaseType.SqlServer
|
|
};
|
|
}
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="db"></param>
|
|
/// <returns></returns>
|
|
|
|
public static string GetDbContextPath(string db)
|
|
{
|
|
if (db == AppDataBaseType.Game.ToString())
|
|
{
|
|
return $"\\{typeof(GameDbContext).Namespace}\\Entities\\Game";
|
|
}
|
|
if (db == AppDataBaseType.Ext.ToString())
|
|
{
|
|
return $"\\{typeof(ExtDbContext).Namespace}\\Entities\\Ext";
|
|
}
|
|
if (db == AppDataBaseType.App.ToString())
|
|
{
|
|
return $"\\{typeof(PhoneDbContext).Namespace}\\Entities\\App";
|
|
}
|
|
if (db == AppDataBaseType.User.ToString())
|
|
{
|
|
return $"\\{typeof(UserDbContext).Namespace}\\Entities\\User";
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public static string GetNamespace(string db)
|
|
{
|
|
if (db == AppDataBaseType.Game.ToString())
|
|
{
|
|
return $"{typeof(GameDbContext).Namespace}.Entities.Game";
|
|
}
|
|
if (db == AppDataBaseType.Ext.ToString())
|
|
{
|
|
return $"{typeof(ExtDbContext).Namespace}.Entities.Ext";
|
|
}
|
|
if (db == AppDataBaseType.App.ToString())
|
|
{
|
|
return $"{typeof(PhoneDbContext).Namespace}.Entities.App";
|
|
}
|
|
if (db == AppDataBaseType.User.ToString())
|
|
{
|
|
return $"{typeof(UserDbContext).Namespace}.Entities.User";
|
|
}
|
|
return null;
|
|
}
|
|
|
|
|
|
|
|
///// <summary>
|
|
/////
|
|
///// </summary>
|
|
///// <returns></returns>
|
|
//public static List<DbTableInfo> GetDbTableInfo(this BaseDbContext baseDbContext)
|
|
//{
|
|
// var list = baseDbContext.UnitOfWork.FreeSqlOrm.DbFirst.GetTablesByDatabase();
|
|
// list.ForEach(item => item.Schema = baseDbContext._appDataBaseType.ToString());
|
|
// return list;
|
|
//}
|
|
|
|
} |