From cd9e27bcf3fcdfc30a705df068662667a2eb8d75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E6=B7=B3?= Date: Tue, 14 Mar 2023 09:22:20 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E6=94=B9=E7=94=A8sqlsugar=20db?= =?UTF-8?q?=E4=B8=8A=E4=B8=8B=E6=96=87=E5=AF=B9=E8=B1=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Extensions/SqlsugarExtensions.cs | 6 ++++-- .../Yi.Framework.Core.Sqlsugar/SqlSugarDbContext.cs | 6 +++--- .../YiFrameworkCoreSqlsugarModule.cs | 4 +++- 3 files changed, 10 insertions(+), 6 deletions(-) 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(); } }