diff --git a/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/SqlsugarCoreExtensions.cs b/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/SqlsugarCoreExtensions.cs index 7269e9d0..cdafb998 100644 --- a/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/SqlsugarCoreExtensions.cs +++ b/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/SqlsugarCoreExtensions.cs @@ -11,22 +11,23 @@ namespace Yi.Framework.SqlSugarCore { public static class SqlsugarCoreExtensions { - public static IServiceCollection AddYiDbContext(this IServiceCollection service) where DbContext : class, ISqlSugarDbContext + public static IServiceCollection AddYiDbContext(this IServiceCollection service, ServiceLifetime serviceLifetime = ServiceLifetime.Transient) where DbContext : class, ISqlSugarDbContext { - service.Replace(new ServiceDescriptor(typeof(ISqlSugarDbContext), typeof(DbContext), ServiceLifetime.Transient)); + service.Replace(new ServiceDescriptor(typeof(ISqlSugarDbContext), typeof(DbContext), serviceLifetime)); return service; } - public static IServiceCollection TryAddYiDbContext(this IServiceCollection service) where DbContext : class, ISqlSugarDbContext + public static IServiceCollection TryAddYiDbContext(this IServiceCollection service, ServiceLifetime serviceLifetime = ServiceLifetime.Transient) where DbContext : class, ISqlSugarDbContext { - service.TryAdd(new ServiceDescriptor(typeof(ISqlSugarDbContext), typeof(DbContext), ServiceLifetime.Transient)); + service.TryAdd(new ServiceDescriptor(typeof(ISqlSugarDbContext), typeof(DbContext), serviceLifetime)); return service; } - public static IServiceCollection AddYiDbContext(this IServiceCollection service,Action options) where DbContext : class, ISqlSugarDbContext + public static IServiceCollection AddYiDbContext(this IServiceCollection service, Action options) where DbContext : class, ISqlSugarDbContext { - - service.Configure(ops => { + + service.Configure(ops => + { options.Invoke(ops); }); service.AddYiDbContext(); diff --git a/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/Uow/UnitOfWorkSqlsugarDbContextProvider.cs b/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/Uow/UnitOfWorkSqlsugarDbContextProvider.cs index 8fe45bae..b76e6c8d 100644 --- a/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/Uow/UnitOfWorkSqlsugarDbContextProvider.cs +++ b/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/Uow/UnitOfWorkSqlsugarDbContextProvider.cs @@ -52,13 +52,13 @@ namespace Yi.Framework.SqlSugarCore.Uow var unitOfWork = UnitOfWorkManager.Current; - if (unitOfWork == null || unitOfWork.Options.IsTransactional == false) + if (unitOfWork == null /*|| unitOfWork.Options.IsTransactional == false*/) { - if (ContextInstance.Current is null) - { - ContextInstance.Current = (TDbContext)ServiceProvider.GetRequiredService(); - } - var dbContext = (TDbContext)ContextInstance.Current; + //if (ContextInstance.Current is null) + //{ + // ContextInstance.Current = ; + //} + var dbContext = (TDbContext)ServiceProvider.GetRequiredService(); //提高体验,取消工作单元强制性 //throw new AbpException("A DbContext can only be created inside a unit of work!"); //如果不启用工作单元,创建一个新的db,不开启事务即可 @@ -81,6 +81,7 @@ namespace Yi.Framework.SqlSugarCore.Uow CreateDbContextAsync(unitOfWork, connectionStringName, connectionString).Result ); + await Console.Out.WriteLineAsync(">>>----------------实例化了db"+ ((SqlSugarDatabaseApi)databaseApi).DbContext.SqlSugarClient.ContextID.ToString()); //创建的db加入到当前工作单元中 unitOfWork.AddDatabaseApi(dbContextKey, databaseApi); diff --git a/Yi.Abp.Net8/src/Yi.Abp.Application/Services/TestService.cs b/Yi.Abp.Net8/src/Yi.Abp.Application/Services/TestService.cs index d3180d46..e8c6bcb2 100644 --- a/Yi.Abp.Net8/src/Yi.Abp.Application/Services/TestService.cs +++ b/Yi.Abp.Net8/src/Yi.Abp.Application/Services/TestService.cs @@ -55,9 +55,10 @@ namespace Yi.Abp.Application.Services //魔改 // 用户体验优先,万金油模式,支持高并发。支持单、多线程并发安全,支持多线程工作单元,支持多线程无工作单元,支持。。。 // 自动在各个情况处理db客户端最优解之一 - int i = 10; + int i = 3; List tasks = new List(); - + await sqlSugarRepository.GetListAsync(); + await sqlSugarRepository.GetListAsync(); while (i > 0) { tasks.Add(Task.Run(async () =>