| | |
| | | using Microsoft.Extensions.Configuration; |
| | | using Microsoft.Extensions.DependencyInjection; |
| | | using SqlSugar; |
| | | using SqlSugar.IOC; |
| | | |
| | | namespace ZTICInterface.Core; |
| | | |
| | | public static class SqlsugarSetup |
| | | { |
| | | public static void AddSqlsugarSetup(this IServiceCollection services, IConfiguration configuration, string dbName = "db_master") |
| | | public static void AddSqlsugarSetup(this IServiceCollection services) |
| | | { |
| | | //如果多个数数据库传 List<ConnectionConfig> |
| | | var configConnection = new ConnectionConfig() |
| | | |
| | | services.AddSqlSugar(new IocConfig() |
| | | { |
| | | DbType = (DbType)Enum.Parse(typeof(DbType), App.Configuration["DatabaseSettings:Data:DbType"]), |
| | | ConnectionString = App.Configuration["DatabaseSettings:MainDb:ConnectionString"], |
| | | ConnectionString = App.Configuration["DatabaseSettings:MainDb:ConnectionString"],//连接符字串 |
| | | DbType = (IocDbType)Enum.Parse(typeof(IocDbType), App.Configuration["DatabaseSettings:MainDb:DbType"]), |
| | | IsAutoCloseConnection = true, |
| | | }; |
| | | |
| | | SqlSugarScope sqlSugar = new SqlSugarScope(configConnection, |
| | | db => |
| | | ConfigId = "1", |
| | | }); |
| | | |
| | | services.ConfigurationSugar(db => |
| | | { |
| | | db.GetConnection("1").Aop.OnLogExecuting = (sql, p) => |
| | | { |
| | | //单例参数配置,所有上下文生效 |
| | | db.Aop.OnLogExecuting = (sql, p) => |
| | | { |
| | | Console.WriteLine(SqlProfiler.ParameterFormat(sql, p)); |
| | | Console.WriteLine(); |
| | | App.PrintToMiniProfiler("SqlSugar", "Info", SqlProfiler.ParameterFormat(sql, p)); |
| | | }; |
| | | }); |
| | | |
| | | services.AddSingleton<ISqlSugarClient>(sqlSugar);//这边是SqlSugarScope用AddSingleton |
| | | Console.WriteLine(SqlProfiler.ParameterFormat(sql, p)); |
| | | Console.WriteLine(); |
| | | App.PrintToMiniProfiler("SqlSugar", "Info", SqlProfiler.ParameterFormat(sql, p)); |
| | | }; |
| | | |
| | | }); |
| | | } |
| | | |
| | | |