基础服务api
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.
 
 

45 lines
1.7 KiB

  1. using Furion;
  2. using Microsoft.Extensions.Configuration;
  3. using Microsoft.Extensions.DependencyInjection;
  4. using SqlSugar;
  5. using System;
  6. using System.Collections.Generic;
  7. namespace BPA.SAAS.Manage.Core
  8. {
  9. /// <summary>
  10. /// 数据库上下文对象
  11. /// </summary>
  12. public static class DbContext
  13. {
  14. /// <summary>
  15. /// SqlSugar 数据库实例
  16. /// </summary>
  17. public static void AddSqlsugarSetup(this IServiceCollection services, IConfiguration configuration)
  18. {
  19. var connectionConfigs = App.GetConfig<List<ConnectionConfig>>("ConnectionConfigs");
  20. //如果多个数数据库传 List<ConnectionConfig>
  21. var configConnection = new ConnectionConfig()
  22. {
  23. ConnectionString = connectionConfigs[0].ConnectionString,
  24. DbType = DbType.MySql,//设置数据库类型
  25. IsAutoCloseConnection = true,//自动释放数据务,如果存在事务,在事务结束后释放
  26. InitKeyType = InitKeyType.Attribute, //从实体特性中读取主键自增列信息
  27. ConfigId = "default",
  28. };
  29. SqlSugarScope sqlSugar = new SqlSugarScope(configConnection,
  30. db =>
  31. {
  32. //单例参数配置,所有上下文生效
  33. db.Aop.OnLogExecuting = (sql, pars) =>
  34. {
  35. Console.WriteLine(sql);//输出sql
  36. //Console.WriteLine($"当前SQL语句:【{sql}】,参数:【{string.Join(",", pars.Select(t => t.Value))}】");
  37. };
  38. });
  39. services.AddSingleton<ISqlSugarClient>(sqlSugar);//这边是SqlSugarScope用AddSingleton
  40. }
  41. }
  42. }