平安校园
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3 月之前
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. 
  2. //
  3. namespace SafeCampus.SqlSugar;
  4. /// <summary>
  5. /// AppStartup启动类
  6. /// </summary>
  7. [AppStartup(98)]
  8. public class Startup : AppStartup
  9. {
  10. /// <summary>
  11. /// ConfigureServices中不能解析服务,比如App.GetService(),尤其是不能在ConfigureServices中获取诸如缓存等数据进行初始化,应该在Configure中进行
  12. /// 服务都还没初始化完成,会导致内存中存在多份 IOC 容器!!
  13. /// 正确应该在 Configure 中,这个时候服务(IServiceCollection 已经完成 BuildServiceProvider() 操作了
  14. /// </summary>
  15. /// <param name="services"></param>
  16. public void ConfigureServices(IServiceCollection services)
  17. {
  18. //检查ConfigId
  19. CheckSameConfigId();
  20. if (StaticConfig.AppContext_ConvertInfinityDateTime == false)
  21. {
  22. AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);
  23. AppContext.SetSwitch("Npgsql.DisableDateTimeInfinityConversions", true);
  24. }
  25. }
  26. public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
  27. {
  28. //遍历配置
  29. DbContext.DB_CONFIGS.ForEach(it =>
  30. {
  31. var connection = DbContext.DB.GetConnection(it.ConfigId);//获取数据库连接对象
  32. connection.DbMaintenance.CreateDatabase();//创建数据库,如果存在则不创建
  33. });
  34. }
  35. /// <summary>
  36. /// 检查是否有相同的ConfigId
  37. /// </summary>
  38. /// <returns></returns>
  39. private static void CheckSameConfigId()
  40. {
  41. var configIdGroup = DbContext.DB_CONFIGS.GroupBy(it => it.ConfigId).ToList();
  42. foreach (var configId in configIdGroup)
  43. {
  44. if (configId.ToList().Count > 1) throw Oops.Oh($"SqlSugar连接配置ConfigId:{configId.Key}重复了");
  45. }
  46. }
  47. }