From 822ab4e7c7cc1029e1f1d7dcf950e974f4bf84ea Mon Sep 17 00:00:00 2001 From: xuejiaming <326308290@qq.com> Date: Fri, 9 Dec 2022 22:20:16 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Defcore2=E7=9A=84=E5=88=86?= =?UTF-8?q?=E5=BA=93bug=E5=92=8C=E7=A7=BB=E9=99=A4=E6=B3=9B=E5=9E=8B?= =?UTF-8?q?=E7=BA=A6=E6=9D=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/WeatherForecastController.cs | 5 +++- samples/Sample.SqlServer/DbContextHelper.cs | 2 +- .../Tx/ShardingRelationalTransaction.cs | 26 +++++++++++++++---- .../ShardingRelationalTransactionFactory.cs | 15 +++-------- .../ShardingRelationalTransactionManager.cs | 26 ++++++++++++------- .../ShardingRelationalTransactionFactory.cs | 2 +- .../ShardingRelationalTransactionManager.cs | 2 +- .../ShardingRelationalTransactionFactory.cs | 2 +- .../ShardingRelationalTransactionManager.cs | 2 +- .../ShardingRelationalTransactionFactory.cs | 2 +- .../ShardingRelationalTransactionManager.cs | 3 ++- .../ShardingRelationalTransactionFactory.cs | 2 +- .../ShardingRelationalTransactionManager.cs | 2 +- src/ShardingCore/ShardingCoreExtension.cs | 9 +++---- 14 files changed, 58 insertions(+), 42 deletions(-) diff --git a/samples/Sample.MySql/Controllers/WeatherForecastController.cs b/samples/Sample.MySql/Controllers/WeatherForecastController.cs index 537cfa60..6dd9e34f 100644 --- a/samples/Sample.MySql/Controllers/WeatherForecastController.cs +++ b/samples/Sample.MySql/Controllers/WeatherForecastController.cs @@ -73,7 +73,10 @@ namespace Sample.MySql.Controllers [HttpGet] public async Task Get() { - + using (var dbContextTransaction = _defaultTableDbContext.Database.BeginTransaction()) + { + + } var x2 = await (from ut in _defaultTableDbContext.Set() join uu in _defaultTableDbContext.Set() diff --git a/samples/Sample.SqlServer/DbContextHelper.cs b/samples/Sample.SqlServer/DbContextHelper.cs index 6d1f2331..81af438b 100644 --- a/samples/Sample.SqlServer/DbContextHelper.cs +++ b/samples/Sample.SqlServer/DbContextHelper.cs @@ -11,7 +11,7 @@ namespace Sample.SqlServer public static DbContext CreateDbContextByString(string connectionString,IShardingRuntimeContext shardingRuntimeContext) { var dbContextOptionsBuilder = new DbContextOptionsBuilder(); - dbContextOptionsBuilder.UseSqlServer(connectionString).UseSharding(shardingRuntimeContext); + dbContextOptionsBuilder.UseSqlServer(connectionString).UseSharding(shardingRuntimeContext); return new DefaultShardingDbContext(dbContextOptionsBuilder.Options); } } diff --git a/src/ShardingCore/EFCores/EFCore2x/Tx/ShardingRelationalTransaction.cs b/src/ShardingCore/EFCores/EFCore2x/Tx/ShardingRelationalTransaction.cs index 33a5c45d..3007725e 100644 --- a/src/ShardingCore/EFCores/EFCore2x/Tx/ShardingRelationalTransaction.cs +++ b/src/ShardingCore/EFCores/EFCore2x/Tx/ShardingRelationalTransaction.cs @@ -23,16 +23,32 @@ namespace ShardingCore.EFCores */ public class ShardingRelationalTransaction : RelationalTransaction { - private readonly IShardingDbContext _shardingDbContext; - private readonly IShardingDbContextExecutor _shardingDbContextExecutor; - public ShardingRelationalTransaction(IShardingDbContext shardingDbContext, IRelationalConnection connection, DbTransaction transaction,IDiagnosticsLogger logger, bool transactionOwned) : base(connection, transaction, logger, transactionOwned) + private IShardingDbContext _shardingDbContext; + private IShardingDbContextExecutor _shardingDbContextExecutor; + private bool _inited = false; + public ShardingRelationalTransaction(IRelationalConnection connection, DbTransaction transaction,IDiagnosticsLogger logger, bool transactionOwned) : base(connection, transaction, logger, transactionOwned) { + + } + + private void CheckInit() + { + if (_inited) + { + throw new ShardingCoreInvalidOperationException($"cant repat init {nameof(ShardingRelationalTransaction)}"); + } + + } + public void SetShardingDbContext(IShardingDbContext shardingDbContext) + { + CheckInit(); _shardingDbContext = - shardingDbContext??throw new ShardingCoreInvalidOperationException($"should implement {nameof(IShardingDbContext)}"); + shardingDbContext??throw new ShardingCoreInvalidOperationException($"should implement {nameof(IShardingDbContext)}"); _shardingDbContextExecutor = shardingDbContext.GetShardingExecutor() ?? throw new ShardingCoreInvalidOperationException( $"{shardingDbContext.GetType()} cant get {nameof(IShardingDbContextExecutor)} from {nameof(shardingDbContext.GetShardingExecutor)}"); - + + _inited = true; } //protected override void ClearTransaction() diff --git a/src/ShardingCore/EFCores/EFCore2x/Tx/ShardingRelationalTransactionFactory.cs b/src/ShardingCore/EFCores/EFCore2x/Tx/ShardingRelationalTransactionFactory.cs index 3e146823..f5e8a7d0 100644 --- a/src/ShardingCore/EFCores/EFCore2x/Tx/ShardingRelationalTransactionFactory.cs +++ b/src/ShardingCore/EFCores/EFCore2x/Tx/ShardingRelationalTransactionFactory.cs @@ -21,7 +21,7 @@ namespace ShardingCore.EFCores */ - public class ShardingRelationalTransactionFactory : RelationalTransactionFactory where TShardingDbContext : DbContext, IShardingDbContext + public class ShardingRelationalTransactionFactory: RelationalTransactionFactory { private readonly RelationalTransactionFactoryDependencies _dependencies; public ShardingRelationalTransactionFactory(RelationalTransactionFactoryDependencies dependencies) : base(dependencies) @@ -32,18 +32,9 @@ namespace ShardingCore.EFCores , IDiagnosticsLogger logger, bool transactionOwned) { - var shardingDbContext = GetDbContext(connection) as IShardingDbContext; - return new ShardingRelationalTransaction(shardingDbContext, connection, transaction, logger, + return new ShardingRelationalTransaction(connection, transaction, logger, transactionOwned); } - - private DbContext GetDbContext(IRelationalConnection connection) - { - var namedConnectionStringResolver = ((RelationalConnectionDependencies)connection.GetPropertyValue("Dependencies")).ConnectionStringResolver; - var serviceProvider = (IServiceProvider)namedConnectionStringResolver.GetPropertyValue("ApplicationServiceProvider"); - var dbContext = (DbContext)serviceProvider.GetService(typeof(TShardingDbContext)); - return dbContext; - } } } -#endif +#endif \ No newline at end of file diff --git a/src/ShardingCore/EFCores/EFCore2x/Tx/ShardingRelationalTransactionManager.cs b/src/ShardingCore/EFCores/EFCore2x/Tx/ShardingRelationalTransactionManager.cs index 5daf7a61..c2e50491 100644 --- a/src/ShardingCore/EFCores/EFCore2x/Tx/ShardingRelationalTransactionManager.cs +++ b/src/ShardingCore/EFCores/EFCore2x/Tx/ShardingRelationalTransactionManager.cs @@ -7,6 +7,7 @@ using System.Text; using System.Threading; using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Internal; using Microsoft.EntityFrameworkCore.Storage; using ShardingCore.Exceptions; using ShardingCore.Extensions; @@ -24,27 +25,22 @@ namespace ShardingCore.EFCores /// /// manage transaction /// - public class ShardingRelationalTransactionManager : IRelationalTransactionManager where TShardingDbContext : DbContext, IShardingDbContext + public class ShardingRelationalTransactionManager : IRelationalTransactionManager { private readonly IRelationalConnection _relationalConnection; + private readonly ICurrentDbContext _currentDbContext; private readonly IShardingDbContext _shardingDbContext; private readonly IShardingDbContextExecutor _shardingDbContextExecutor; - public ShardingRelationalTransactionManager(IRelationalConnection relationalConnection) + public ShardingRelationalTransactionManager(IRelationalConnection relationalConnection,ICurrentDbContext currentDbContext) { _relationalConnection = relationalConnection; - _shardingDbContext = GetDbContext(relationalConnection) as IShardingDbContext??throw new ShardingCoreInvalidOperationException($"should implement {nameof(IShardingDbContext)}"); + _currentDbContext = currentDbContext; + _shardingDbContext = currentDbContext.Context as IShardingDbContext??throw new ShardingCoreInvalidOperationException($"should implement {nameof(IShardingDbContext)}"); _shardingDbContextExecutor = _shardingDbContext.GetShardingExecutor(); } - private DbContext GetDbContext(IRelationalConnection connection) - { - var namedConnectionStringResolver = ((RelationalConnectionDependencies)connection.GetPropertyValue("Dependencies")).ConnectionStringResolver; - var serviceProvider = (IServiceProvider)namedConnectionStringResolver.GetPropertyValue("ApplicationServiceProvider"); - var dbContext = (DbContext)serviceProvider.GetService(typeof(TShardingDbContext)); - return dbContext; - } public void ResetState() { @@ -77,6 +73,11 @@ namespace ShardingCore.EFCores public IDbContextTransaction BeginTransaction(IsolationLevel isolationLevel) { var dbContextTransaction = _relationalConnection.BeginTransaction(isolationLevel); + + if (dbContextTransaction is ShardingRelationalTransaction shardingRelationalTransaction) + { + shardingRelationalTransaction.SetShardingDbContext(_shardingDbContext); + } _shardingDbContextExecutor.NotifyShardingTransaction(); return dbContextTransaction; } @@ -85,6 +86,11 @@ namespace ShardingCore.EFCores CancellationToken cancellationToken = new CancellationToken()) { var dbContextTransaction = await _relationalConnection.BeginTransactionAsync(isolationLevel, cancellationToken); + + if (dbContextTransaction is ShardingRelationalTransaction shardingRelationalTransaction) + { + shardingRelationalTransaction.SetShardingDbContext(_shardingDbContext); + } _shardingDbContextExecutor.NotifyShardingTransaction(); return dbContextTransaction; } diff --git a/src/ShardingCore/EFCores/EFCore3x/Tx/ShardingRelationalTransactionFactory.cs b/src/ShardingCore/EFCores/EFCore3x/Tx/ShardingRelationalTransactionFactory.cs index e93b616b..d3fa90d3 100644 --- a/src/ShardingCore/EFCores/EFCore3x/Tx/ShardingRelationalTransactionFactory.cs +++ b/src/ShardingCore/EFCores/EFCore3x/Tx/ShardingRelationalTransactionFactory.cs @@ -20,7 +20,7 @@ namespace ShardingCore.EFCores * @Email: 326308290@qq.com */ - public class ShardingRelationalTransactionFactory : RelationalTransactionFactory where TShardingDbContext : DbContext, IShardingDbContext + public class ShardingRelationalTransactionFactory: RelationalTransactionFactory { private readonly RelationalTransactionFactoryDependencies _dependencies; public ShardingRelationalTransactionFactory(RelationalTransactionFactoryDependencies dependencies) : base(dependencies) diff --git a/src/ShardingCore/EFCores/EFCore3x/Tx/ShardingRelationalTransactionManager.cs b/src/ShardingCore/EFCores/EFCore3x/Tx/ShardingRelationalTransactionManager.cs index b812fe7c..43101cb6 100644 --- a/src/ShardingCore/EFCores/EFCore3x/Tx/ShardingRelationalTransactionManager.cs +++ b/src/ShardingCore/EFCores/EFCore3x/Tx/ShardingRelationalTransactionManager.cs @@ -24,7 +24,7 @@ namespace ShardingCore.EFCores /// /// manage transaction /// - public class ShardingRelationalTransactionManager : IRelationalTransactionManager where TShardingDbContext : DbContext, IShardingDbContext + public class ShardingRelationalTransactionManager: IRelationalTransactionManager { private readonly IRelationalConnection _relationalConnection; private readonly IShardingDbContext _shardingDbContext; diff --git a/src/ShardingCore/EFCores/EFCore5x/Tx/ShardingRelationalTransactionFactory.cs b/src/ShardingCore/EFCores/EFCore5x/Tx/ShardingRelationalTransactionFactory.cs index 596f75e3..97f55a6b 100644 --- a/src/ShardingCore/EFCores/EFCore5x/Tx/ShardingRelationalTransactionFactory.cs +++ b/src/ShardingCore/EFCores/EFCore5x/Tx/ShardingRelationalTransactionFactory.cs @@ -21,7 +21,7 @@ namespace ShardingCore.EFCores */ - public class ShardingRelationalTransactionFactory : RelationalTransactionFactory where TShardingDbContext : DbContext, IShardingDbContext + public class ShardingRelationalTransactionFactory : RelationalTransactionFactory { private readonly RelationalTransactionFactoryDependencies _dependencies; public ShardingRelationalTransactionFactory(RelationalTransactionFactoryDependencies dependencies) : base(dependencies) diff --git a/src/ShardingCore/EFCores/EFCore5x/Tx/ShardingRelationalTransactionManager.cs b/src/ShardingCore/EFCores/EFCore5x/Tx/ShardingRelationalTransactionManager.cs index 85ad6ac3..e9d642eb 100644 --- a/src/ShardingCore/EFCores/EFCore5x/Tx/ShardingRelationalTransactionManager.cs +++ b/src/ShardingCore/EFCores/EFCore5x/Tx/ShardingRelationalTransactionManager.cs @@ -24,7 +24,7 @@ namespace ShardingCore.EFCores /// /// manage transaction /// - public class ShardingRelationalTransactionManager : IRelationalTransactionManager where TShardingDbContext : DbContext, IShardingDbContext + public class ShardingRelationalTransactionManager : IRelationalTransactionManager { private readonly IRelationalConnection _relationalConnection; private readonly IShardingDbContext _shardingDbContext; diff --git a/src/ShardingCore/EFCores/EFCore6x/Tx/ShardingRelationalTransactionFactory.cs b/src/ShardingCore/EFCores/EFCore6x/Tx/ShardingRelationalTransactionFactory.cs index 6bb9862d..ecb4c23b 100644 --- a/src/ShardingCore/EFCores/EFCore6x/Tx/ShardingRelationalTransactionFactory.cs +++ b/src/ShardingCore/EFCores/EFCore6x/Tx/ShardingRelationalTransactionFactory.cs @@ -21,7 +21,7 @@ namespace ShardingCore.EFCores */ - public class ShardingRelationalTransactionFactory : RelationalTransactionFactory where TShardingDbContext : DbContext, IShardingDbContext + public class ShardingRelationalTransactionFactory : RelationalTransactionFactory { private readonly RelationalTransactionFactoryDependencies _dependencies; public ShardingRelationalTransactionFactory(RelationalTransactionFactoryDependencies dependencies) : base(dependencies) diff --git a/src/ShardingCore/EFCores/EFCore6x/Tx/ShardingRelationalTransactionManager.cs b/src/ShardingCore/EFCores/EFCore6x/Tx/ShardingRelationalTransactionManager.cs index 9d32ff38..b0069667 100644 --- a/src/ShardingCore/EFCores/EFCore6x/Tx/ShardingRelationalTransactionManager.cs +++ b/src/ShardingCore/EFCores/EFCore6x/Tx/ShardingRelationalTransactionManager.cs @@ -8,6 +8,7 @@ using System.Text; using System.Threading; using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Storage; using ShardingCore.Exceptions; using ShardingCore.Extensions; @@ -25,7 +26,7 @@ namespace ShardingCore.EFCores /// /// manage transaction /// - public class ShardingRelationalTransactionManager : IRelationalTransactionManager where TShardingDbContext : DbContext, IShardingDbContext + public class ShardingRelationalTransactionManager : IRelationalTransactionManager { private readonly IRelationalConnection _relationalConnection; private readonly IShardingDbContext _shardingDbContext; diff --git a/src/ShardingCore/EFCores/EFCore7x/Tx/ShardingRelationalTransactionFactory.cs b/src/ShardingCore/EFCores/EFCore7x/Tx/ShardingRelationalTransactionFactory.cs index 59e1079e..07465490 100644 --- a/src/ShardingCore/EFCores/EFCore7x/Tx/ShardingRelationalTransactionFactory.cs +++ b/src/ShardingCore/EFCores/EFCore7x/Tx/ShardingRelationalTransactionFactory.cs @@ -21,7 +21,7 @@ namespace ShardingCore.EFCores */ - public class ShardingRelationalTransactionFactory : RelationalTransactionFactory where TShardingDbContext : DbContext, IShardingDbContext + public class ShardingRelationalTransactionFactory : RelationalTransactionFactory { private readonly RelationalTransactionFactoryDependencies _dependencies; public ShardingRelationalTransactionFactory(RelationalTransactionFactoryDependencies dependencies) : base(dependencies) diff --git a/src/ShardingCore/EFCores/EFCore7x/Tx/ShardingRelationalTransactionManager.cs b/src/ShardingCore/EFCores/EFCore7x/Tx/ShardingRelationalTransactionManager.cs index 7cca8b72..a1e84ee5 100644 --- a/src/ShardingCore/EFCores/EFCore7x/Tx/ShardingRelationalTransactionManager.cs +++ b/src/ShardingCore/EFCores/EFCore7x/Tx/ShardingRelationalTransactionManager.cs @@ -24,7 +24,7 @@ namespace ShardingCore.EFCores /// /// manage transaction /// - public class ShardingRelationalTransactionManager : IRelationalTransactionManager where TShardingDbContext : DbContext, IShardingDbContext + public class ShardingRelationalTransactionManager : IRelationalTransactionManager { private readonly IRelationalConnection _relationalConnection; private readonly IShardingDbContext _shardingDbContext; diff --git a/src/ShardingCore/ShardingCoreExtension.cs b/src/ShardingCore/ShardingCoreExtension.cs index 0b8aa840..b194a0bf 100644 --- a/src/ShardingCore/ShardingCoreExtension.cs +++ b/src/ShardingCore/ShardingCoreExtension.cs @@ -126,7 +126,7 @@ namespace ShardingCore var contextOptionsBuilder = virtualDataSource.ConfigurationParams .UseDbContextOptionsBuilder(connectionString, dbContextOptionsBuilder) .UseShardingMigrator() - .UseSharding(shardingRuntimeContext); + .UseSharding(shardingRuntimeContext); virtualDataSource.ConfigurationParams.UseShellDbContextOptionBuilder(contextOptionsBuilder); return dbContextOptionsBuilder; @@ -210,18 +210,17 @@ namespace ShardingCore return services; } - public static DbContextOptionsBuilder UseSharding( + public static DbContextOptionsBuilder UseSharding( this DbContextOptionsBuilder optionsBuilder, IShardingRuntimeContext shardingRuntimeContext) - where TShardingDbContext : DbContext, IShardingDbContext { return optionsBuilder.UseShardingWrapMark().UseShardingOptions(shardingRuntimeContext) .ReplaceService() .ReplaceService() .ReplaceService() .ReplaceService>() + ShardingRelationalTransactionManager>() .ReplaceService>(); + ShardingRelationalTransactionFactory>(); } public static DbContextOptionsBuilder UseShardingMigrator(