diff --git a/Yi.Framework.Net6/src/framework/Yi.Framework.Core.Sqlsugar/Extensions/SqlsugarExtensions.cs b/Yi.Framework.Net6/src/framework/Yi.Framework.Core.Sqlsugar/Extensions/SqlsugarExtensions.cs index 8ef4f8ac..165d8eea 100644 --- a/Yi.Framework.Net6/src/framework/Yi.Framework.Core.Sqlsugar/Extensions/SqlsugarExtensions.cs +++ b/Yi.Framework.Net6/src/framework/Yi.Framework.Core.Sqlsugar/Extensions/SqlsugarExtensions.cs @@ -24,12 +24,14 @@ namespace Yi.Framework.Core.Sqlsugar.Extensions /// public static class SqlsugarExtensions { + //使用上下文对象 public static void AddDbSqlsugarContextServer(this IServiceCollection services) { - services.AddTransient(x => x.GetRequiredService().SqlSugarClient); - services.AddTransient(); + services.AddSingleton(x => x.GetRequiredService().SqlSugarClient); + services.AddSingleton(); } + //直接使用sqlsugar public static void AddSqlsugarServer(this IServiceCollection services, Action? action = null) { var dbConnOptions = Appsettings.app("DbConnOptions"); diff --git a/Yi.Framework.Net6/src/framework/Yi.Framework.Core.Sqlsugar/SqlSugarDbContext.cs b/Yi.Framework.Net6/src/framework/Yi.Framework.Core.Sqlsugar/SqlSugarDbContext.cs index b15a509e..e83bdb34 100644 --- a/Yi.Framework.Net6/src/framework/Yi.Framework.Core.Sqlsugar/SqlSugarDbContext.cs +++ b/Yi.Framework.Net6/src/framework/Yi.Framework.Core.Sqlsugar/SqlSugarDbContext.cs @@ -21,7 +21,7 @@ namespace Yi.Framework.Core.Sqlsugar /// /// SqlSugar 客户端 /// - public ISqlSugarClient SqlSugarClient { get; set; } + public ISqlSugarClient SqlSugarClient { get; set; } protected ICurrentUser _currentUser; @@ -33,7 +33,7 @@ namespace Yi.Framework.Core.Sqlsugar { _currentUser = currentUser; _logger = logger; - _options= options; + _options = options; var dbConnOptions = options.Value; #region 组装options if (dbConnOptions.DbType is null) @@ -57,7 +57,7 @@ namespace Yi.Framework.Core.Sqlsugar }); } #endregion - ISqlSugarClient sqlSugar = new SqlSugarClient(new ConnectionConfig() + SqlSugarClient = new SqlSugarScope(new ConnectionConfig() { //准备添加分表分库 DbType = dbConnOptions.DbType ?? DbType.Sqlite, diff --git a/Yi.Framework.Net6/src/framework/Yi.Framework.Core.Sqlsugar/YiFrameworkCoreSqlsugarModule.cs b/Yi.Framework.Net6/src/framework/Yi.Framework.Core.Sqlsugar/YiFrameworkCoreSqlsugarModule.cs index de0e3ae5..01a744f2 100644 --- a/Yi.Framework.Net6/src/framework/Yi.Framework.Core.Sqlsugar/YiFrameworkCoreSqlsugarModule.cs +++ b/Yi.Framework.Net6/src/framework/Yi.Framework.Core.Sqlsugar/YiFrameworkCoreSqlsugarModule.cs @@ -28,7 +28,9 @@ namespace Yi.Framework.Core.Sqlsugar services.Replace(new ServiceDescriptor(typeof(IUnitOfWorkManager), typeof(SqlsugarUnitOfWorkManager), ServiceLifetime.Singleton)); services.Replace(new ServiceDescriptor(typeof(IDataFilter), typeof(SqlsugarDataFilter), ServiceLifetime.Scoped)); services.Configure(Appsettings.appConfiguration("DbConnOptions")); - services.AddSqlsugarServer(); + + //使用db上下文 + services.AddDbSqlsugarContextServer(); } }