using System;
using Microsoft.Extensions.DependencyInjection;
using SqlSugar;
using gpt_api.Respository;
namespace gpt_api.Web.Core.Setup
{
///
/// Sqlsugar 扩展集成
///
public static class SqlsugarSetup
{
public static void AddSqlsugarSetup(this IServiceCollection services, string conStr)
{
var configConnection = new ConnectionConfig()
{
DbType = DbType.MySql,
ConnectionString = conStr,
IsAutoCloseConnection = true,
InitKeyType = InitKeyType.Attribute,//从特性读取主键自增信息
};
SqlSugarScope sqlSugar = new SqlSugarScope(configConnection);
sqlSugar.Ado.CommandTimeOut = 10;//单位 秒
sqlSugar.QueryFilter.Add(new SqlFilterItem//单表全局过滤器
{
FilterValue = _filterDb =>
{
return new SqlFilterResult()
{
Sql = " IsDeleted!=1"
};
},
IsJoinQuery = false
})
.Add(new SqlFilterItem//多表全局过滤器
{
FilterValue = _filterDb =>
{
return new SqlFilterResult()
{
Sql = " f.IsDeleted=0"
};
},
IsJoinQuery = true
});
services.AddSingleton(sqlSugar); // 单例注册
services.AddScoped(typeof(IRepository<>), typeof(Repository<>));//仓储注册
services.AddUnitOfWork();//启用sqlsugar工作单元(仓储事务)
}
}
}