平安校园
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.
 
 
 
 
 
 

49 lines
1.8 KiB

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