namespace SafeCampus.SqlSugar;
///
/// AppStartup启动类
///
[AppStartup(98)]
public class Startup : AppStartup
{
///
/// ConfigureServices中不能解析服务,比如App.GetService(),尤其是不能在ConfigureServices中获取诸如缓存等数据进行初始化,应该在Configure中进行
/// 服务都还没初始化完成,会导致内存中存在多份 IOC 容器!!
/// 正确应该在 Configure 中,这个时候服务(IServiceCollection 已经完成 BuildServiceProvider() 操作了
///
///
public void ConfigureServices(IServiceCollection services)
{
//检查ConfigId
CheckSameConfigId();
if (StaticConfig.AppContext_ConvertInfinityDateTime == false)
{
AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);
AppContext.SetSwitch("Npgsql.DisableDateTimeInfinityConversions", true);
}
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
//遍历配置
DbContext.DB_CONFIGS.ForEach(it =>
{
var connection = DbContext.DB.GetConnection(it.ConfigId);//获取数据库连接对象
connection.DbMaintenance.CreateDatabase();//创建数据库,如果存在则不创建
});
}
///
/// 检查是否有相同的ConfigId
///
///
private static void CheckSameConfigId()
{
var configIdGroup = DbContext.DB_CONFIGS.GroupBy(it => it.ConfigId).ToList();
foreach (var configId in configIdGroup)
{
if (configId.ToList().Count > 1) throw Oops.Oh($"SqlSugar连接配置ConfigId:{configId.Key}重复了");
}
}
}