数据库连接替换成IOC配置
This commit is contained in:
@@ -11,7 +11,11 @@ using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.OpenApi.Models;
|
||||
using SqlSugar.IOC;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using ZR.Admin.WebApi.Extensions;
|
||||
using ZR.Admin.WebApi.Filters;
|
||||
using ZR.Admin.WebApi.Middleware;
|
||||
@@ -116,12 +120,54 @@ namespace ZR.Admin.WebApi
|
||||
/// ע<><D7A2>Services<65><73><EFBFBD><EFBFBD>
|
||||
/// </summary>
|
||||
/// <param name="services"></param>
|
||||
private static void InjectRepositories(IServiceCollection services)
|
||||
private void InjectRepositories(IServiceCollection services)
|
||||
{
|
||||
services.AddAppService();
|
||||
|
||||
//<2F><><EFBFBD><EFBFBD><EFBFBD>ƻ<EFBFBD><C6BB><EFBFBD><EFBFBD><EFBFBD>
|
||||
services.AddTaskSchedulers();
|
||||
|
||||
string connStr = Configuration.GetConnectionString(OptionsSetting.ConnAdmin);
|
||||
string connStrBus = Configuration.GetConnectionString(OptionsSetting.ConnBus);
|
||||
string dbKey = Configuration[OptionsSetting.DbKey];
|
||||
int dbType = Convert.ToInt32(Configuration[OptionsSetting.ConnDbType]);
|
||||
int dbType_bus = Convert.ToInt32(Configuration[OptionsSetting.ConnBusDbType]);
|
||||
|
||||
IocConfig db1 = new IocConfig()
|
||||
{
|
||||
ConfigId = "0", //<2F><><EFBFBD>⻧<EFBFBD>õ<EFBFBD>
|
||||
ConnectionString = connStr,
|
||||
DbType = (IocDbType)dbType,
|
||||
IsAutoCloseConnection = true//<2F>Զ<EFBFBD><D4B6>ͷ<EFBFBD>
|
||||
};
|
||||
IocConfig db2 = new IocConfig()
|
||||
{
|
||||
ConfigId = "1", // <20><><EFBFBD>⻧<EFBFBD>õ<EFBFBD>
|
||||
ConnectionString = connStrBus,
|
||||
DbType = (IocDbType)dbType_bus,
|
||||
IsAutoCloseConnection = true//<2F>Զ<EFBFBD><D4B6>ͷ<EFBFBD>
|
||||
};
|
||||
SugarIocServices.AddSqlSugar(new List<IocConfig>() { db1, db2 });
|
||||
|
||||
//<2F><>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӡSQL
|
||||
DbScoped.SugarScope.GetConnection(0).Aop.OnLogExecuting = (sql, pars) =>
|
||||
{
|
||||
Console.BackgroundColor = ConsoleColor.Yellow;
|
||||
Console.WriteLine("<22><>SQL<51><4C><EFBFBD>䡿" + sql.ToLower() + "\r\n"
|
||||
+ DbScoped.SugarScope.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
|
||||
};
|
||||
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӡ<EFBFBD><D3A1>־
|
||||
DbScoped.SugarScope.GetConnection(0).Aop.OnError = (e) =>
|
||||
{
|
||||
Console.WriteLine($"[ִ<><D6B4>Sql<71><6C><EFBFBD><EFBFBD>]{e.Message}<7D><>SQL={e.Sql}");
|
||||
Console.WriteLine();
|
||||
};
|
||||
//Bus Db<44><62><EFBFBD><EFBFBD><EFBFBD><EFBFBD>־
|
||||
DbScoped.SugarScope.GetConnection(1).Aop.OnError = (e) =>
|
||||
{
|
||||
Console.WriteLine($"[ִ<><D6B4>Sql<71><6C><EFBFBD><EFBFBD>Bus]{e.Message}<7D><>SQL={e.Sql}");
|
||||
Console.WriteLine();
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user