Files
shgx_tz_mes_backend_sync/ZR.Repository/DbProvider/SugarDbContext.cs

51 lines
1.7 KiB
C#
Raw Normal View History

2021-08-23 16:57:25 +08:00
using Infrastructure;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
namespace ZR.Repository.DbProvider
{
/// <summary>
/// SqlSugar ORM
/// </summary>
public class SugarDbContext
{
public SqlSugarClient Db; //用来处理事务多表查询和复杂的操作
/// <summary>
/// 使用SugarSql获取连接对象
/// </summary>
/// <returns></returns>
public SugarDbContext()
{
string connStr = ConfigUtils.Instance.GetConnectionStrings(OptionsSetting.ConnAdmin);
int dbType = ConfigUtils.Instance.GetAppConfig<int>(OptionsSetting.ConnDbType);
2021-09-06 21:37:32 +08:00
2021-08-23 16:57:25 +08:00
Db = new SqlSugarClient(new List<ConnectionConfig>()
{
new ConnectionConfig(){
ConnectionString = connStr,
DbType = (DbType)dbType,
2021-08-23 16:57:25 +08:00
IsAutoCloseConnection = true,//开启自动释放模式和EF原理一样
InitKeyType = InitKeyType.Attribute,//从特性读取主键和自增列信息
ConfigId = 0
},
});
2021-09-27 08:06:09 +08:00
////调式代码 用来打印SQL
//Db.Aop.OnLogExecuting = (sql, pars) =>
//{
// Console.BackgroundColor = ConsoleColor.Yellow;
// Console.WriteLine("【SQL语句】" + sql.ToLower() + "\r\n" + Db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
//};
////出错打印日志
//Db.Aop.OnError = (e) =>
//{
// Console.WriteLine($"[执行Sql出错]{e.Message}SQL={e.Sql}");
// Console.WriteLine();
//};
2021-08-23 16:57:25 +08:00
}
}
}