You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
1.7 KiB
C#

using Estsh.Core.Config;
using SqlSugar;
using System;
using System.Collections.Generic;
namespace Estsh.Core.SqlSugar
{
/// <summary>
/// SqlSugar数据库上下文
/// </summary>
public class SqlSugarDbHelper
{
/// <summary>
/// 系统单例上下文对象
/// </summary>
public static SqlSugarScope SqlSugarDbContext = new SqlSugarScope(new ConnectionConfig()
{
DbType = (DbType) GlobalConfig.DbConfig.DbType,
InitKeyType = InitKeyType.Attribute,
IsAutoCloseConnection = true,
ConnectionString = GlobalConfig.DbConfig.ConnectionString//主库
},
db =>
{
//单例参数配置,所有上下文生效
db.Ado.CommandTimeOut = GlobalConfig.DbConfig.CommandTimeOut;//数据库超时时间,单位秒
//SQL执行前回调函数
db.Aop.OnLogExecuting = (sql, pars) =>
{
//执行前可以输出SQL
//Console.WriteLine(sql);
};
//修改SQL和参数的值
db.Aop.OnExecutingChangeSql = (sql, pars) =>
{
if (!String.IsNullOrEmpty(sql) && sql.Contains("CreateTime"))
{
sql = sql.Replace("CreateTime", "CREATE_TIME");
}
return new KeyValuePair<string, SugarParameter[]>(sql, pars);
};
//SQL执行完回调函数
db.Aop.OnLogExecuted = (sql, pars) =>
{
//执行完可以输出SQL执行时间
//Console.Write($"SQL:{sql},\r\nTimeSpan:{db.Ado.SqlExecutionTime.TotalMilliseconds}ms\r\n");
};
});
}
}