数据库连接替换成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();
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,6 +32,7 @@
|
||||
<PackageReference Include="NLog" Version="4.7.5" />
|
||||
<PackageReference Include="NLog.Web.AspNetCore" Version="4.9.3" />
|
||||
<PackageReference Include="Snowflake.Core" Version="2.0.0" />
|
||||
<PackageReference Include="SqlSugar.IOC" Version="1.7.0" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.1.4" />
|
||||
<PackageReference Include="UAParser" Version="3.1.46" />
|
||||
</ItemGroup>
|
||||
|
||||
@@ -7,9 +7,11 @@
|
||||
}
|
||||
},
|
||||
"ConnectionStrings": {
|
||||
"conn_zrAdmin": "server=127.0.0.1;user=zr;pwd=abc;database=admin"
|
||||
"conn_zrAdmin": "server=127.0.0.1;user=zr;pwd=abc;database=admin",
|
||||
"conn_bus": "server=127.0.0.1;user=zr;pwd=abc;database=admin"
|
||||
},
|
||||
"conn_zrAdmin_Type": 0, //MySql = 0, SqlServer = 1
|
||||
"conn_zrAdmin_type": 0, //MySql = 0, SqlServer = 1
|
||||
"conn_bus_type": 0,
|
||||
"urls": "http://localhost:8888", //<2F><>Ŀ<EFBFBD><C4BF><EFBFBD><EFBFBD>url
|
||||
"sysConfig": {
|
||||
"DBCommandTimeout": 10,
|
||||
|
||||
Reference in New Issue
Block a user