73 lines
2.9 KiB
C#
73 lines
2.9 KiB
C#
using JNPF;
|
|
using JNPF.DatabaseAccessor;
|
|
using SqlSugar;
|
|
|
|
namespace Microsoft.Extensions.DependencyInjection;
|
|
|
|
/// <summary>
|
|
/// SqlSugar配置拓展.
|
|
/// </summary>
|
|
public static class ConfigureSqlSugarExtensions
|
|
{
|
|
public static IServiceCollection ConfigureSqlSugar(this IServiceCollection services)
|
|
{
|
|
// 获取选项
|
|
ConnectionStringsOptions conn = App.GetConfig<ConnectionStringsOptions>("ConnectionStrings", true);
|
|
|
|
List<ConnectionConfig> connectConfigList = new List<ConnectionConfig>();
|
|
var DBType = (DbType)Enum.Parse(typeof(DbType), conn.DBType);
|
|
// 默认数据库
|
|
connectConfigList.Add(new ConnectionConfig
|
|
{
|
|
ConnectionString = conn.ConnectString,
|
|
DbType = DBType,
|
|
IsAutoCloseConnection = true,
|
|
ConfigId = conn.ConfigId,
|
|
InitKeyType = InitKeyType.Attribute,
|
|
MoreSettings = new ConnMoreSettings()
|
|
{
|
|
IsAutoRemoveDataCache = true, // 自动清理缓存
|
|
IsAutoToUpper = false,
|
|
//PgSqlIsAutoToLower = false,
|
|
DisableNvarchar = true
|
|
},
|
|
});
|
|
|
|
services.AddSqlSugar(connectConfigList, client =>
|
|
{
|
|
//connectConfigList.ForEach(config =>
|
|
//{
|
|
// var db = ((SqlSugarScope)client).GetConnectionScope((string)config.ConfigId);
|
|
|
|
// // 设置超时时间
|
|
// db.Ado.CommandTimeOut = 30;
|
|
// db.Aop.OnLogExecuted = (sql, pars) =>
|
|
// {
|
|
// var oldColor = Console.ForegroundColor;
|
|
// Console.ForegroundColor = ConsoleColor.Green;
|
|
// var finalSql = UtilMethods.GetSqlString(db.CurrentConnectionConfig.DbType, sql, pars);
|
|
// Console.WriteLine($"【{DateTime.Now.ToString("HH:mm:ss.fff")}——SQL执行完成】{db.Ado.SqlExecutionTime.TotalMilliseconds} ms");
|
|
// if (db.Ado.SqlExecutionTime.TotalMilliseconds > 3000)
|
|
// {
|
|
// Log.Warning($"慢查询: {db.Ado.SqlExecutionTime.TotalMilliseconds}ms, SQL: " + finalSql);
|
|
// }
|
|
// else
|
|
// {
|
|
// Console.WriteLine(finalSql);
|
|
// Console.ForegroundColor = oldColor;
|
|
// Console.WriteLine();
|
|
// }
|
|
// };
|
|
// db.Aop.OnError = (ex) =>
|
|
// {
|
|
// Log.Error(UtilMethods.GetSqlString(db.CurrentConnectionConfig.DbType, ex.Sql, (SugarParameter[])ex.Parametres));
|
|
// };
|
|
//});
|
|
});
|
|
services.AddUnitOfWork<SqlSugarUnitOfWork>();
|
|
services.AddConfigurableOptions<ConnectionStringsOptions>();
|
|
services.AddConfigurableOptions<TenantOptions>();
|
|
|
|
return services;
|
|
}
|
|
} |