34 lines
1.1 KiB
C#
34 lines
1.1 KiB
C#
#region
|
|
|
|
using SqlSugar;
|
|
using Web.Core.Common.Helper;
|
|
|
|
#endregion
|
|
|
|
namespace Web.Core.Repository.sugar;
|
|
|
|
public class DbContext<T> where T : class, new()
|
|
{
|
|
public DbContext()
|
|
{
|
|
Db = new SqlSugarClient(new ConnectionConfig
|
|
{
|
|
//ConnectionString = BaseDBConfig.ConnectionString,
|
|
ConnectionString = AppSettings.GetConnectionString("DataFill"),
|
|
DbType = DbType.SqlServer,
|
|
InitKeyType = InitKeyType.Attribute, //从特性读取主键和自增列信息
|
|
IsAutoCloseConnection = true //开启自动释放模式和EF原理一样我就不多解释了
|
|
});
|
|
//调式代码 用来打印SQL
|
|
Db.Aop.OnLogExecuting = (sql, pars) =>
|
|
{
|
|
Console.WriteLine(sql + "\r\n" +
|
|
Db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
|
|
Console.WriteLine();
|
|
};
|
|
}
|
|
|
|
//注意:不能写成静态的
|
|
public SqlSugarClient Db; //用来处理事务多表查询和复杂的操作
|
|
public SimpleClient<T> CurrentDb => new(Db); //用来操作当前表的数据
|
|
} |