diff --git a/samples/Sample.MySql/Startup.cs b/samples/Sample.MySql/Startup.cs index 6741a042..baa4f457 100644 --- a/samples/Sample.MySql/Startup.cs +++ b/samples/Sample.MySql/Startup.cs @@ -131,7 +131,7 @@ namespace Sample.MySql { b.ReplaceService(); }); - }).ReplaceService() + }) .AddShardingCore(); // services.AddDbContext(ShardingCoreExtension // .UseMutliDefaultSharding); diff --git a/samples/Samples.AbpSharding/AbstractShardingAbpDbContext.cs b/samples/Samples.AbpSharding/AbstractShardingAbpDbContext.cs index 68e00e96..dda7e16b 100644 --- a/samples/Samples.AbpSharding/AbstractShardingAbpDbContext.cs +++ b/samples/Samples.AbpSharding/AbstractShardingAbpDbContext.cs @@ -22,7 +22,7 @@ using ShardingCore.Sharding; namespace Samples.AbpSharding { - public abstract class AbstractShardingAbpDbContext : AbpDbContext, IShardingDbContext, ISupportShardingReadWrite + public abstract class AbstractShardingAbpDbContext : AbpDbContext, IShardingDbContext where TDbContext : DbContext { private readonly IShardingDbContextExecutor _shardingDbContextExecutor; @@ -571,5 +571,9 @@ namespace Samples.AbpSharding return _shardingDbContextExecutor.GetVirtualDataSource(); } + public IDictionary GetCurrentDbContexts() + { + return _shardingDbContextExecutor.GetCurrentDbContexts(); + } } } \ No newline at end of file diff --git a/samples/Samples.AbpSharding/AbstractShardingAbpZeroDbContext.cs b/samples/Samples.AbpSharding/AbstractShardingAbpZeroDbContext.cs index 82f633f8..6a24723a 100644 --- a/samples/Samples.AbpSharding/AbstractShardingAbpZeroDbContext.cs +++ b/samples/Samples.AbpSharding/AbstractShardingAbpZeroDbContext.cs @@ -28,9 +28,7 @@ namespace Samples.AbpSharding { public abstract class AbstractShardingAbpZeroDbContext : AbpZeroDbContext, - IShardingDbContext, - ISupportShardingReadWrite, - IShardingTableDbContext + IShardingDbContext where TTenant : AbpTenant where TRole : AbpRole where TUser : AbpUser @@ -650,5 +648,9 @@ namespace Samples.AbpSharding #endregion + public IDictionary GetCurrentDbContexts() + { + return _shardingDbContextExecutor.GetCurrentDbContexts(); + } } } diff --git a/src/ShardingCore/Bootstrappers/ShardingInitializer.cs b/src/ShardingCore/Bootstrappers/ShardingInitializer.cs index 70b20903..2a297d38 100644 --- a/src/ShardingCore/Bootstrappers/ShardingInitializer.cs +++ b/src/ShardingCore/Bootstrappers/ShardingInitializer.cs @@ -2,6 +2,7 @@ using System.Linq; using System.Threading.Tasks; using Microsoft.Extensions.Logging; using ShardingCore.Core.DbContextCreator; +using ShardingCore.Core.DbContextTypeAwares; using ShardingCore.Core.EntityMetadatas; using ShardingCore.Core.ServiceProviders; using ShardingCore.Core.ShardingConfigurations.Abstractions; @@ -9,7 +10,7 @@ using ShardingCore.Exceptions; using ShardingCore.Extensions; using ShardingCore.Jobs; using ShardingCore.Jobs.Abstaractions; - +using ShardingCore.Sharding.Abstractions; using ShardingCore.Sharding.MergeEngines.ParallelControl; using ShardingCore.Sharding.ParallelTables; @@ -20,18 +21,25 @@ namespace ShardingCore.Bootstrappers { private readonly ILogger _logger; private readonly IShardingProvider _shardingProvider; + private readonly IDbContextTypeAware _dbContextTypeAware; private readonly IShardingRouteConfigOptions _routeConfigOptions; private readonly IEntityMetadataManager _entityMetadataManager; private readonly IParallelTableManager _parallelTableManager; private readonly DoOnlyOnce _doOnlyOnce = new DoOnlyOnce(); - public ShardingInitializer(IShardingProvider shardingProvider,ILogger logger) + public ShardingInitializer(IShardingProvider shardingProvider, + IDbContextTypeAware dbContextTypeAware, + IShardingRouteConfigOptions shardingRouteConfigOptions, + IEntityMetadataManager entityMetadataManager, + IParallelTableManager parallelTableManager, + ILogger logger) { _logger = logger; _shardingProvider = shardingProvider; - _routeConfigOptions = shardingProvider.GetRequiredService(); - _entityMetadataManager = shardingProvider.GetRequiredService(); - _parallelTableManager = shardingProvider.GetRequiredService(); + _dbContextTypeAware = dbContextTypeAware; + _routeConfigOptions = shardingRouteConfigOptions; + _entityMetadataManager = entityMetadataManager; + _parallelTableManager = parallelTableManager; } /// /// 初始化 @@ -63,6 +71,18 @@ namespace ShardingCore.Bootstrappers var entityMetadataInitializer =(IEntityMetadataInitializer)_shardingProvider.CreateInstance(entityMetadataInitializerType); entityMetadataInitializer.Initialize(); } + + var allShardingEntities = _entityMetadataManager.GetAllShardingEntities(); + //判断如果有分表的对象那么dbcontext必须继承IShardingTableDbContext + if (allShardingEntities.Any(o => _entityMetadataManager.IsShardingTable(o))) + { + var contextType = _dbContextTypeAware.GetContextType(); + if (!contextType.IsShardingTableDbContext()) + { + throw new ShardingCoreInvalidOperationException( + $"db context {contextType},has sharding table route,should be implement {nameof(IShardingTableDbContext)}"); + } + } } private void InitializeParallelTables() diff --git a/src/ShardingCore/Core/DbContextTypeAwares/DbContextTypeAware.cs b/src/ShardingCore/Core/DbContextTypeAwares/DbContextTypeAware.cs new file mode 100644 index 00000000..68c57171 --- /dev/null +++ b/src/ShardingCore/Core/DbContextTypeAwares/DbContextTypeAware.cs @@ -0,0 +1,17 @@ +using System; + +namespace ShardingCore.Core.DbContextTypeAwares; + +public class DbContextTypeAware:IDbContextTypeAware +{ + private readonly Type _dbContextType; + + public DbContextTypeAware(Type dbContextType) + { + _dbContextType = dbContextType; + } + public Type GetContextType() + { + return _dbContextType; + } +} \ No newline at end of file diff --git a/src/ShardingCore/Core/DbContextTypeAwares/IDbContextTypeAware.cs b/src/ShardingCore/Core/DbContextTypeAwares/IDbContextTypeAware.cs new file mode 100644 index 00000000..3030e59a --- /dev/null +++ b/src/ShardingCore/Core/DbContextTypeAwares/IDbContextTypeAware.cs @@ -0,0 +1,8 @@ +using System; + +namespace ShardingCore.Core.DbContextTypeAwares; + +public interface IDbContextTypeAware +{ + Type GetContextType(); +} \ No newline at end of file diff --git a/src/ShardingCore/Core/RuntimeContexts/IShardingRuntimeContext.cs b/src/ShardingCore/Core/RuntimeContexts/IShardingRuntimeContext.cs index 65dcd59a..001ad9d5 100644 --- a/src/ShardingCore/Core/RuntimeContexts/IShardingRuntimeContext.cs +++ b/src/ShardingCore/Core/RuntimeContexts/IShardingRuntimeContext.cs @@ -2,6 +2,7 @@ using System; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging; using ShardingCore.Core.DbContextCreator; +using ShardingCore.Core.DbContextTypeAwares; using ShardingCore.Core.EntityMetadatas; using ShardingCore.Core.QueryRouteManagers.Abstractions; using ShardingCore.Core.QueryTrackers; @@ -28,6 +29,7 @@ namespace ShardingCore.Core.RuntimeContexts public interface IShardingRuntimeContext { Type DbContextType { get; } + IDbContextTypeAware GetDbContextTypeAware(); IShardingProvider GetShardingProvider(); ShardingConfigOptions GetShardingConfigOptions(); IShardingRouteConfigOptions GetShardingRouteConfigOptions(); diff --git a/src/ShardingCore/Core/RuntimeContexts/IShardingRuntimeContextManager.cs b/src/ShardingCore/Core/RuntimeContexts/IShardingRuntimeContextManager.cs deleted file mode 100644 index 48c65a19..00000000 --- a/src/ShardingCore/Core/RuntimeContexts/IShardingRuntimeContextManager.cs +++ /dev/null @@ -1,17 +0,0 @@ -using System; -using System.Collections.Generic; - -namespace ShardingCore.Core.RuntimeContexts -{ - public interface IShardingRuntimeContextManager - { - /// - /// 尝试获取注册的dbcontext type对应的 没有返回null - /// - /// - /// - IShardingRuntimeContext TryGet(Type dbContextType); - - IReadOnlyDictionary GetAll(); - } -} \ No newline at end of file diff --git a/src/ShardingCore/Core/RuntimeContexts/ShardingRuntimeContext.cs b/src/ShardingCore/Core/RuntimeContexts/ShardingRuntimeContext.cs index 377e4460..b6f5cb91 100644 --- a/src/ShardingCore/Core/RuntimeContexts/ShardingRuntimeContext.cs +++ b/src/ShardingCore/Core/RuntimeContexts/ShardingRuntimeContext.cs @@ -4,6 +4,7 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using ShardingCore.Bootstrappers; using ShardingCore.Core.DbContextCreator; +using ShardingCore.Core.DbContextTypeAwares; using ShardingCore.Core.EntityMetadatas; using ShardingCore.Core.QueryRouteManagers.Abstractions; using ShardingCore.Core.QueryTrackers; @@ -72,9 +73,15 @@ namespace ShardingCore.Core.RuntimeContexts { GetRequiredService().AutoShardingCreate(); } + private IDbContextTypeAware _dbContextTypeAware; + public IDbContextTypeAware GetDbContextTypeAware() + { + return _dbContextTypeAware??=GetRequiredService(); + } private IShardingProvider _shardingProvider; + public IShardingProvider GetShardingProvider() { return _shardingProvider??=GetRequiredService(); @@ -314,6 +321,7 @@ namespace ShardingCore.Core.RuntimeContexts private void InitFieldValue() { + GetDbContextTypeAware(); GetShardingProvider(); GetShardingConfigOptions(); GetShardingRouteConfigOptions(); diff --git a/src/ShardingCore/Core/RuntimeContexts/ShardingRuntimeContextManager.cs b/src/ShardingCore/Core/RuntimeContexts/ShardingRuntimeContextManager.cs deleted file mode 100644 index 585aed6e..00000000 --- a/src/ShardingCore/Core/RuntimeContexts/ShardingRuntimeContextManager.cs +++ /dev/null @@ -1,32 +0,0 @@ -using System; -using System.Collections; -using System.Collections.Generic; -using System.Collections.Immutable; -using System.Linq; - -namespace ShardingCore.Core.RuntimeContexts -{ - - public class ShardingRuntimeContextManager:IShardingRuntimeContextManager - { - private readonly ImmutableDictionary _caches; - public ShardingRuntimeContextManager(IEnumerable shardingRuntimeContexts) - { - _caches = shardingRuntimeContexts.ToImmutableDictionary(o => o.DbContextType, o => o); - } - public IShardingRuntimeContext TryGet(Type dbContextType) - { - if (_caches.TryGetValue(dbContextType, out var shardingRuntimeContext)) - { - return shardingRuntimeContext; - } - - return null; - } - - public IReadOnlyDictionary GetAll() - { - return _caches; - } - } -} diff --git a/src/ShardingCore/EFCores/ChangeTrackers/ShardingChangeTracker.cs b/src/ShardingCore/EFCores/ChangeTrackers/ShardingChangeTracker.cs index 01ee4f89..62dbdba6 100644 --- a/src/ShardingCore/EFCores/ChangeTrackers/ShardingChangeTracker.cs +++ b/src/ShardingCore/EFCores/ChangeTrackers/ShardingChangeTracker.cs @@ -26,9 +26,9 @@ namespace ShardingCore.EFCores.ChangeTrackers public override bool HasChanges() { - if (_dbContext is ICurrentDbContextDiscover currentDbContextDiscover) + if (_dbContext is IShardingDbContext shardingDbContext) { - return currentDbContextDiscover.GetCurrentDbContexts().Any(o => + return shardingDbContext.GetCurrentDbContexts().Any(o => o.Value.GetCurrentContexts().Any(r => r.Value.ChangeTracker.HasChanges())); } @@ -37,9 +37,9 @@ namespace ShardingCore.EFCores.ChangeTrackers public override IEnumerable Entries() { - if (_dbContext is ICurrentDbContextDiscover currentDbContextDiscover) + if (_dbContext is IShardingDbContext shardingDbContext) { - return currentDbContextDiscover.GetCurrentDbContexts().SelectMany(o => + return shardingDbContext.GetCurrentDbContexts().SelectMany(o => o.Value.GetCurrentContexts().SelectMany(cd => cd.Value.ChangeTracker.Entries())); } @@ -48,9 +48,9 @@ namespace ShardingCore.EFCores.ChangeTrackers public override IEnumerable> Entries() { - if (_dbContext is ICurrentDbContextDiscover currentDbContextDiscover) + if (_dbContext is IShardingDbContext shardingDbContext) { - return currentDbContextDiscover.GetCurrentDbContexts().SelectMany(o => + return shardingDbContext.GetCurrentDbContexts().SelectMany(o => o.Value.GetCurrentContexts().SelectMany(cd => cd.Value.ChangeTracker.Entries())); } @@ -59,7 +59,7 @@ namespace ShardingCore.EFCores.ChangeTrackers public override void DetectChanges() { - if (_dbContext is ICurrentDbContextDiscover) + if (_dbContext is IShardingDbContext) { Do(c => c.DetectChanges()); return; @@ -69,7 +69,7 @@ namespace ShardingCore.EFCores.ChangeTrackers public override void AcceptAllChanges() { - if (_dbContext is ICurrentDbContextDiscover) + if (_dbContext is IShardingDbContext) { Do(c => c.AcceptAllChanges()); return; @@ -79,7 +79,7 @@ namespace ShardingCore.EFCores.ChangeTrackers private void Do(Action action) { - var dataSourceDbContexts = ((ICurrentDbContextDiscover)_dbContext).GetCurrentDbContexts(); + var dataSourceDbContexts = ((IShardingDbContext)_dbContext).GetCurrentDbContexts(); foreach (var dataSourceDbContext in dataSourceDbContexts) { var currentContexts = dataSourceDbContext.Value.GetCurrentContexts(); @@ -113,7 +113,7 @@ namespace ShardingCore.EFCores.ChangeTrackers public override void CascadeChanges() { - if (_dbContext is ICurrentDbContextDiscover) + if (_dbContext is IShardingDbContext) { Do(c => c.CascadeChanges()); return; @@ -125,7 +125,7 @@ namespace ShardingCore.EFCores.ChangeTrackers #if !NETCOREAPP2_0 && !NETCOREAPP3_0 public override void Clear() { - if (_dbContext is ICurrentDbContextDiscover) + if (_dbContext is IShardingDbContext) { Do(c => c.Clear()); return; diff --git a/src/ShardingCore/EFCores/RelationTransactions/ShardingRelationalTransaction.cs b/src/ShardingCore/EFCores/RelationTransactions/ShardingRelationalTransaction.cs index 0f9ad2d8..76c709a2 100644 --- a/src/ShardingCore/EFCores/RelationTransactions/ShardingRelationalTransaction.cs +++ b/src/ShardingCore/EFCores/RelationTransactions/ShardingRelationalTransaction.cs @@ -29,21 +29,28 @@ namespace ShardingCore.EFCores #if NET6_0 public ShardingRelationalTransaction(IShardingDbContext shardingDbContext, IRelationalConnection connection, DbTransaction transaction, Guid transactionId, IDiagnosticsLogger logger, bool transactionOwned, ISqlGenerationHelper sqlGenerationHelper) : base(connection, transaction, transactionId, logger, transactionOwned, sqlGenerationHelper) { - _shardingDbContext = shardingDbContext ?? throw new ShardingCoreInvalidOperationException($"should implement {nameof(IShardingDbContext)}"); + _shardingDbContext = + shardingDbContext ?? throw new ShardingCoreInvalidOperationException($"should implement {nameof(IShardingDbContext)}"); } #endif #if NETCOREAPP3_0 || NET5_0 - public ShardingRelationalTransaction(IShardingDbContext shardingDbContext, IRelationalConnection connection, DbTransaction transaction, Guid transactionId, IDiagnosticsLogger logger, bool transactionOwned) : base(connection, transaction, transactionId, logger, transactionOwned) + public ShardingRelationalTransaction(IShardingDbContext shardingDbContext, IRelationalConnection connection, + DbTransaction transaction, Guid transactionId, + IDiagnosticsLogger logger, bool transactionOwned) : base(connection, + transaction, transactionId, logger, transactionOwned) { - _shardingDbContext = shardingDbContext??throw new ShardingCoreInvalidOperationException($"should implement {nameof(IShardingDbContext)}"); + _shardingDbContext = shardingDbContext ?? + throw new ShardingCoreInvalidOperationException( + $"should implement {nameof(IShardingDbContext)}"); } #endif #if NETCOREAPP2_0 public ShardingRelationalTransaction(IShardingDbContext shardingDbContext, IRelationalConnection connection, DbTransaction transaction,IDiagnosticsLogger logger, bool transactionOwned) : base(connection, transaction, logger, transactionOwned) { - _shardingDbContext = shardingDbContext??throw new ShardingCoreInvalidOperationException($"should implement {nameof(IShardingDbContext)}"); + _shardingDbContext = + shardingDbContext??throw new ShardingCoreInvalidOperationException($"should implement {nameof(IShardingDbContext)}"); } #endif @@ -54,9 +61,7 @@ namespace ShardingCore.EFCores // base.ClearTransaction(); // _supportShardingTransaction.NotifyShardingTransaction(null); // } - //} - - + //}f public override void Commit() { base.Commit(); @@ -72,7 +77,6 @@ namespace ShardingCore.EFCores } #if !NETCOREAPP2_0 - public override async Task RollbackAsync(CancellationToken cancellationToken = new CancellationToken()) { await base.RollbackAsync(cancellationToken); @@ -88,6 +92,13 @@ namespace ShardingCore.EFCores await _shardingDbContext.CommitAsync(cancellationToken); _shardingDbContext.NotifyShardingTransaction(); } +// #if !NETCOREAPP3_0 +// public override void CreateSavepoint(string name) +// { +// AAA +// base.CreateSavepoint(name); +// } +// #endif #endif } -} +} \ No newline at end of file diff --git a/src/ShardingCore/EFCores/ShardingLocalView.cs b/src/ShardingCore/EFCores/ShardingLocalView.cs index fdccb1f4..08d40902 100644 --- a/src/ShardingCore/EFCores/ShardingLocalView.cs +++ b/src/ShardingCore/EFCores/ShardingLocalView.cs @@ -21,7 +21,7 @@ namespace ShardingCore.EFCores public override IEnumerator GetEnumerator() { - if (_dbContext is ICurrentDbContextDiscover currentDbContextDiscover) + if (_dbContext is IShardingDbContext currentDbContextDiscover) { var dataSourceDbContexts = currentDbContextDiscover.GetCurrentDbContexts(); var enumerators = dataSourceDbContexts.SelectMany(o => o.Value.GetCurrentContexts().Select(cd=>cd.Value.Set().Local.GetEnumerator())); diff --git a/src/ShardingCore/Extensions/ShardingReadWriteExtension.cs b/src/ShardingCore/Extensions/ShardingReadWriteExtension.cs index 88144fba..51cf5844 100644 --- a/src/ShardingCore/Extensions/ShardingReadWriteExtension.cs +++ b/src/ShardingCore/Extensions/ShardingReadWriteExtension.cs @@ -20,7 +20,6 @@ namespace ShardingCore.Extensions */ public static class ShardingReadWriteExtension { - /// /// 设置读写分离读取写数据库 /// @@ -28,14 +27,10 @@ namespace ShardingCore.Extensions /// public static bool ReadWriteSeparationWriteOnly(this IShardingDbContext shardingDbContext) { - if (shardingDbContext is ISupportShardingReadWrite supportShardingReadWrite) - { - supportShardingReadWrite.SetReadWriteSeparation(false); - return true; - } - - return false; + shardingDbContext.SetReadWriteSeparation(false); + return true; } + /// /// 设置读写分离读读数据库 /// @@ -43,23 +38,19 @@ namespace ShardingCore.Extensions /// public static bool ReadWriteSeparationReadOnly(this IShardingDbContext shardingDbContext) { - if (shardingDbContext is ISupportShardingReadWrite supportShardingReadWrite) - { - supportShardingReadWrite.SetReadWriteSeparation(true); - return true; - } - - return false; + shardingDbContext.SetReadWriteSeparation(true); + return true; } + /// /// 设置读写分离 /// /// /// 是否是读数据源 - private static void SetReadWriteSeparation(this ISupportShardingReadWrite supportShardingReadWrite, bool readOnly) + private static void SetReadWriteSeparation(this IShardingDbContext supportShardingReadWrite, bool readOnly) { var shardingRuntimeContext = ((DbContext)supportShardingReadWrite).GetShardingRuntimeContext(); - var shardingReadWriteManager =shardingRuntimeContext.GetService(); + var shardingReadWriteManager = shardingRuntimeContext.GetService(); var shardingReadWriteContext = shardingReadWriteManager.GetCurrent(); if (shardingReadWriteContext != null) { @@ -68,13 +59,17 @@ namespace ShardingCore.Extensions supportShardingReadWrite.ReadWriteSeparationPriority = shardingReadWriteContext.DefaultPriority + 1; } } + supportShardingReadWrite.ReadWriteSeparation = readOnly; } - public static void SetReadWriteSeparation(this ShardingReadWriteContext shardingReadWriteContext,int priority, bool readOnly) + + public static void SetReadWriteSeparation(this ShardingReadWriteContext shardingReadWriteContext, int priority, + bool readOnly) { shardingReadWriteContext.DefaultPriority = priority; shardingReadWriteContext.DefaultReadEnable = readOnly; } + /// /// 当前dbcontext是否是启用了读写分离 /// @@ -82,30 +77,27 @@ namespace ShardingCore.Extensions /// public static bool CurrentIsReadWriteSeparation(this IShardingDbContext shardingDbContext) { - if (shardingDbContext is ISupportShardingReadWrite shardingReadWrite) + if (shardingDbContext.IsUseReadWriteSeparation()) { - if (shardingDbContext.IsUseReadWriteSeparation()) + var shardingRuntimeContext = ((DbContext)shardingDbContext).GetShardingRuntimeContext(); + var shardingReadWriteManager = shardingRuntimeContext.GetService(); + var shardingReadWriteContext = shardingReadWriteManager.GetCurrent(); + if (shardingReadWriteContext != null) { - var shardingRuntimeContext = ((DbContext)shardingDbContext).GetShardingRuntimeContext(); - var shardingReadWriteManager =shardingRuntimeContext.GetService(); - var shardingReadWriteContext = shardingReadWriteManager.GetCurrent(); - if (shardingReadWriteContext != null) + if (shardingReadWriteContext.DefaultPriority > shardingDbContext.ReadWriteSeparationPriority) { - if (shardingReadWriteContext.DefaultPriority > shardingReadWrite.ReadWriteSeparationPriority) - { - return shardingReadWriteContext.DefaultReadEnable; - } - else - { - return shardingReadWrite.ReadWriteSeparation; - } + return shardingReadWriteContext.DefaultReadEnable; + } + else + { + return shardingDbContext.ReadWriteSeparation; } - - return shardingReadWrite.ReadWriteSeparation; } + + return shardingDbContext.ReadWriteSeparation; } return false; } } -} +} \ No newline at end of file diff --git a/src/ShardingCore/Sharding/AbstractShardingDbContext.cs b/src/ShardingCore/Sharding/AbstractShardingDbContext.cs index f4fbf98f..677b333b 100644 --- a/src/ShardingCore/Sharding/AbstractShardingDbContext.cs +++ b/src/ShardingCore/Sharding/AbstractShardingDbContext.cs @@ -25,7 +25,7 @@ namespace ShardingCore.Sharding /// /// 分表分库的dbcontext /// - public abstract class AbstractShardingDbContext : DbContext, IShardingDbContext, ISupportShardingReadWrite,ICurrentDbContextDiscover + public abstract class AbstractShardingDbContext : DbContext, IShardingDbContext { protected IShardingDbContextExecutor ShardingDbContextExecutor { get; } @@ -429,7 +429,7 @@ namespace ShardingCore.Sharding { using (var tran = await Database.BeginTransactionAsync(cancellationToken)) { - i = await ShardingDbContextExecutor.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken); + i = await ShardingDbContextExecutor.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken); #if NETCOREAPP2_0 tran.Commit(); #endif diff --git a/src/ShardingCore/Sharding/Abstractions/IShardingDbContext.cs b/src/ShardingCore/Sharding/Abstractions/IShardingDbContext.cs index 47cda99e..0d48fb9a 100644 --- a/src/ShardingCore/Sharding/Abstractions/IShardingDbContext.cs +++ b/src/ShardingCore/Sharding/Abstractions/IShardingDbContext.cs @@ -11,7 +11,7 @@ namespace ShardingCore.Sharding.Abstractions * @Date: Saturday, 14 August 2021 21:47:11 * @Email: 326308290@qq.com */ - public interface IShardingDbContext: IShardingTransaction + public interface IShardingDbContext: IShardingTransaction,ISupportShardingReadWrite,ICurrentDbContextDiscover { /// /// create DbContext diff --git a/src/ShardingCore/ShardingCoreExtension.cs b/src/ShardingCore/ShardingCoreExtension.cs index a76e8ca9..c588dc51 100644 --- a/src/ShardingCore/ShardingCoreExtension.cs +++ b/src/ShardingCore/ShardingCoreExtension.cs @@ -31,6 +31,7 @@ using Microsoft.EntityFrameworkCore.ChangeTracking.Internal; using Microsoft.EntityFrameworkCore.Migrations; using ShardingCore.Bootstrappers; using ShardingCore.Core.DbContextCreator; +using ShardingCore.Core.DbContextTypeAwares; using ShardingCore.Core.QueryTrackers; using ShardingCore.Core.RuntimeContexts; using ShardingCore.Core.ShardingConfigurations.ConfigBuilders; @@ -93,22 +94,6 @@ namespace ShardingCore return services.AddShardingConfigure(); } - public static ShardingCoreConfigBuilder AddMultiShardingDbContext( - this IServiceCollection services, - ServiceLifetime contextLifetime = ServiceLifetime.Scoped, - ServiceLifetime optionsLifetime = ServiceLifetime.Scoped) - where TShardingDbContext : DbContext, IShardingDbContext - { - if (contextLifetime == ServiceLifetime.Singleton) - throw new NotSupportedException($"{nameof(contextLifetime)}:{nameof(ServiceLifetime.Singleton)}"); - if (optionsLifetime == ServiceLifetime.Singleton) - throw new NotSupportedException($"{nameof(optionsLifetime)}:{nameof(ServiceLifetime.Singleton)}"); - services.AddDbContext(UseMutliDefaultSharding, contextLifetime, - optionsLifetime); - services.TryAddSingleton(); - return services.AddShardingConfigure(); - } - public static ShardingCoreConfigBuilder AddShardingConfigure( this IServiceCollection services) where TShardingDbContext : DbContext, IShardingDbContext @@ -131,20 +116,6 @@ namespace ShardingCore dbContextOptionsBuilder.UseDefaultSharding(shardingRuntimeContext); } - public static void UseMutliDefaultSharding(IServiceProvider serviceProvider, - DbContextOptionsBuilder dbContextOptionsBuilder) where TShardingDbContext : DbContext, IShardingDbContext - { - var shardingRuntimeContextManager = serviceProvider.GetRequiredService(); - var shardingRuntimeContext = shardingRuntimeContextManager.TryGet(typeof(TShardingDbContext)); - if (shardingRuntimeContext == null) - { - throw new InvalidOperationException( - $"cant get multi sharding runtime context:[{typeof(TShardingDbContext)}]"); - } - - dbContextOptionsBuilder.UseDefaultSharding(shardingRuntimeContext); - } - public static DbContextOptionsBuilder UseDefaultSharding(this DbContextOptionsBuilder dbContextOptionsBuilder, IShardingRuntimeContext shardingRuntimeContext) where TShardingDbContext : DbContext, IShardingDbContext { @@ -164,6 +135,7 @@ namespace ShardingCore internal static IServiceCollection AddInternalShardingCore(this IServiceCollection services) where TShardingDbContext : DbContext, IShardingDbContext { + services.TryAddSingleton(sp=>new DbContextTypeAware(typeof(TShardingDbContext))); services.TryAddSingleton(); services.TryAddSingleton(); services.TryAddSingleton(); diff --git a/src/ShardingCore/TableExists/EmptyTableEnsureManager.cs b/src/ShardingCore/TableExists/EmptyTableEnsureManager.cs deleted file mode 100644 index 6d085adb..00000000 --- a/src/ShardingCore/TableExists/EmptyTableEnsureManager.cs +++ /dev/null @@ -1,19 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Microsoft.EntityFrameworkCore; -using ShardingCore.Sharding.Abstractions; -using ShardingCore.TableExists.Abstractions; - -namespace ShardingCore.TableExists -{ - public class EmptyTableEnsureManager : ITableEnsureManager - { - public ISet GetExistTables(IShardingDbContext shardingDbContext, string dataSourceName) - { - return new HashSet(); - } - } -}