平安校园
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

59 строки
1.8 KiB

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