123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- using System;
- using Microsoft.Extensions.DependencyInjection;
- using SqlSugar;
- using gpt_api.Respository;
- namespace gpt_api.Web.Core.Setup
- {
- /// <summary>
- /// Sqlsugar 扩展集成
- /// </summary>
- 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<ISqlSugarClient>(sqlSugar); // 单例注册
- services.AddScoped(typeof(IRepository<>), typeof(Repository<>));//仓储注册
- services.AddUnitOfWork<SqlSugarUnitOfWork>();//启用sqlsugar工作单元(仓储事务)
- }
- }
- }
|