添加dbcontext类型发现接口用来判断如果存在分表路由那么必须是分表数据库上下文[#182],将多个接口合并到IShardingDbContext接口上,并且默认的ShellDbContext接口支持track的直接调用
This commit is contained in:
parent
c528a4a83c
commit
244bf061d2
|
@ -131,7 +131,7 @@ namespace Sample.MySql
|
|||
{
|
||||
b.ReplaceService<IMigrationsSqlGenerator, ShardingMySqlMigrationsSqlGenerator>();
|
||||
});
|
||||
}).ReplaceService<ITableEnsureManager, MySqlTableEnsureManager>()
|
||||
})
|
||||
.AddShardingCore();
|
||||
// services.AddDbContext<DefaultShardingDbContext>(ShardingCoreExtension
|
||||
// .UseMutliDefaultSharding<DefaultShardingDbContext>);
|
||||
|
|
|
@ -22,7 +22,7 @@ using ShardingCore.Sharding;
|
|||
namespace Samples.AbpSharding
|
||||
{
|
||||
|
||||
public abstract class AbstractShardingAbpDbContext<TDbContext> : AbpDbContext<TDbContext>, IShardingDbContext, ISupportShardingReadWrite
|
||||
public abstract class AbstractShardingAbpDbContext<TDbContext> : AbpDbContext<TDbContext>, IShardingDbContext
|
||||
where TDbContext : DbContext
|
||||
{
|
||||
private readonly IShardingDbContextExecutor _shardingDbContextExecutor;
|
||||
|
@ -571,5 +571,9 @@ namespace Samples.AbpSharding
|
|||
return _shardingDbContextExecutor.GetVirtualDataSource();
|
||||
}
|
||||
|
||||
public IDictionary<string, IDataSourceDbContext> GetCurrentDbContexts()
|
||||
{
|
||||
return _shardingDbContextExecutor.GetCurrentDbContexts();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -28,9 +28,7 @@ namespace Samples.AbpSharding
|
|||
{
|
||||
public abstract class AbstractShardingAbpZeroDbContext<TTenant, TRole, TUser, TSelf>
|
||||
: AbpZeroDbContext<TTenant, TRole, TUser, TSelf>,
|
||||
IShardingDbContext,
|
||||
ISupportShardingReadWrite,
|
||||
IShardingTableDbContext
|
||||
IShardingDbContext
|
||||
where TTenant : AbpTenant<TUser>
|
||||
where TRole : AbpRole<TUser>
|
||||
where TUser : AbpUser<TUser>
|
||||
|
@ -650,5 +648,9 @@ namespace Samples.AbpSharding
|
|||
|
||||
#endregion
|
||||
|
||||
public IDictionary<string, IDataSourceDbContext> GetCurrentDbContexts()
|
||||
{
|
||||
return _shardingDbContextExecutor.GetCurrentDbContexts();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<ShardingBootstrapper> _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<ShardingBootstrapper> logger)
|
||||
public ShardingInitializer(IShardingProvider shardingProvider,
|
||||
IDbContextTypeAware dbContextTypeAware,
|
||||
IShardingRouteConfigOptions shardingRouteConfigOptions,
|
||||
IEntityMetadataManager entityMetadataManager,
|
||||
IParallelTableManager parallelTableManager,
|
||||
ILogger<ShardingBootstrapper> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
_shardingProvider = shardingProvider;
|
||||
_routeConfigOptions = shardingProvider.GetRequiredService<IShardingRouteConfigOptions>();
|
||||
_entityMetadataManager = shardingProvider.GetRequiredService<IEntityMetadataManager>();
|
||||
_parallelTableManager = shardingProvider.GetRequiredService<IParallelTableManager>();
|
||||
_dbContextTypeAware = dbContextTypeAware;
|
||||
_routeConfigOptions = shardingRouteConfigOptions;
|
||||
_entityMetadataManager = entityMetadataManager;
|
||||
_parallelTableManager = parallelTableManager;
|
||||
}
|
||||
/// <summary>
|
||||
/// 初始化
|
||||
|
@ -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()
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
using System;
|
||||
|
||||
namespace ShardingCore.Core.DbContextTypeAwares;
|
||||
|
||||
public interface IDbContextTypeAware
|
||||
{
|
||||
Type GetContextType();
|
||||
}
|
|
@ -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();
|
||||
|
|
|
@ -1,17 +0,0 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace ShardingCore.Core.RuntimeContexts
|
||||
{
|
||||
public interface IShardingRuntimeContextManager
|
||||
{
|
||||
/// <summary>
|
||||
/// 尝试获取注册的dbcontext type对应的 没有返回null
|
||||
/// </summary>
|
||||
/// <param name="dbContextType"></param>
|
||||
/// <returns></returns>
|
||||
IShardingRuntimeContext TryGet(Type dbContextType);
|
||||
|
||||
IReadOnlyDictionary<Type, IShardingRuntimeContext> GetAll();
|
||||
}
|
||||
}
|
|
@ -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<IShardingBootstrapper>().AutoShardingCreate();
|
||||
}
|
||||
private IDbContextTypeAware _dbContextTypeAware;
|
||||
public IDbContextTypeAware GetDbContextTypeAware()
|
||||
{
|
||||
return _dbContextTypeAware??=GetRequiredService<IDbContextTypeAware>();
|
||||
}
|
||||
|
||||
private IShardingProvider _shardingProvider;
|
||||
|
||||
|
||||
public IShardingProvider GetShardingProvider()
|
||||
{
|
||||
return _shardingProvider??=GetRequiredService<IShardingProvider>();
|
||||
|
@ -314,6 +321,7 @@ namespace ShardingCore.Core.RuntimeContexts
|
|||
|
||||
private void InitFieldValue()
|
||||
{
|
||||
GetDbContextTypeAware();
|
||||
GetShardingProvider();
|
||||
GetShardingConfigOptions();
|
||||
GetShardingRouteConfigOptions();
|
||||
|
|
|
@ -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<Type, IShardingRuntimeContext> _caches;
|
||||
public ShardingRuntimeContextManager(IEnumerable<IShardingRuntimeContext> 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<Type, IShardingRuntimeContext> GetAll()
|
||||
{
|
||||
return _caches;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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<EntityEntry> 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<EntityEntry<TEntity>> Entries<TEntity>()
|
||||
{
|
||||
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<TEntity>()));
|
||||
}
|
||||
|
||||
|
@ -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<ChangeTracker> 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;
|
||||
|
|
|
@ -29,21 +29,28 @@ namespace ShardingCore.EFCores
|
|||
#if NET6_0
|
||||
public ShardingRelationalTransaction(IShardingDbContext shardingDbContext, IRelationalConnection connection, DbTransaction transaction, Guid transactionId, IDiagnosticsLogger<DbLoggerCategory.Database.Transaction> 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<DbLoggerCategory.Database.Transaction> logger, bool transactionOwned) : base(connection, transaction, transactionId, logger, transactionOwned)
|
||||
public ShardingRelationalTransaction(IShardingDbContext shardingDbContext, IRelationalConnection connection,
|
||||
DbTransaction transaction, Guid transactionId,
|
||||
IDiagnosticsLogger<DbLoggerCategory.Database.Transaction> 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<DbLoggerCategory.Database.Transaction> 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
|
||||
}
|
||||
}
|
||||
}
|
|
@ -21,7 +21,7 @@ namespace ShardingCore.EFCores
|
|||
|
||||
public override IEnumerator<TEntity> 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<TEntity>().Local.GetEnumerator()));
|
||||
|
|
|
@ -20,7 +20,6 @@ namespace ShardingCore.Extensions
|
|||
*/
|
||||
public static class ShardingReadWriteExtension
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// 设置读写分离读取写数据库
|
||||
/// </summary>
|
||||
|
@ -28,14 +27,10 @@ namespace ShardingCore.Extensions
|
|||
/// <returns></returns>
|
||||
public static bool ReadWriteSeparationWriteOnly(this IShardingDbContext shardingDbContext)
|
||||
{
|
||||
if (shardingDbContext is ISupportShardingReadWrite supportShardingReadWrite)
|
||||
{
|
||||
supportShardingReadWrite.SetReadWriteSeparation(false);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
shardingDbContext.SetReadWriteSeparation(false);
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置读写分离读读数据库
|
||||
/// </summary>
|
||||
|
@ -43,23 +38,19 @@ namespace ShardingCore.Extensions
|
|||
/// <returns></returns>
|
||||
public static bool ReadWriteSeparationReadOnly(this IShardingDbContext shardingDbContext)
|
||||
{
|
||||
if (shardingDbContext is ISupportShardingReadWrite supportShardingReadWrite)
|
||||
{
|
||||
supportShardingReadWrite.SetReadWriteSeparation(true);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
shardingDbContext.SetReadWriteSeparation(true);
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置读写分离
|
||||
/// </summary>
|
||||
/// <param name="supportShardingReadWrite"></param>
|
||||
/// <param name="readOnly">是否是读数据源</param>
|
||||
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<IShardingReadWriteManager>();
|
||||
var shardingReadWriteManager = shardingRuntimeContext.GetService<IShardingReadWriteManager>();
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 当前dbcontext是否是启用了读写分离
|
||||
/// </summary>
|
||||
|
@ -82,30 +77,27 @@ namespace ShardingCore.Extensions
|
|||
/// <returns></returns>
|
||||
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<IShardingReadWriteManager>();
|
||||
var shardingReadWriteContext = shardingReadWriteManager.GetCurrent();
|
||||
if (shardingReadWriteContext != null)
|
||||
{
|
||||
var shardingRuntimeContext = ((DbContext)shardingDbContext).GetShardingRuntimeContext();
|
||||
var shardingReadWriteManager =shardingRuntimeContext.GetService<IShardingReadWriteManager>();
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -25,7 +25,7 @@ namespace ShardingCore.Sharding
|
|||
/// <summary>
|
||||
/// 分表分库的dbcontext
|
||||
/// </summary>
|
||||
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
|
||||
|
|
|
@ -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
|
||||
{
|
||||
/// <summary>
|
||||
/// create DbContext
|
||||
|
|
|
@ -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<TShardingDbContext>();
|
||||
}
|
||||
|
||||
public static ShardingCoreConfigBuilder<TShardingDbContext> AddMultiShardingDbContext<TShardingDbContext>(
|
||||
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<TShardingDbContext>(UseMutliDefaultSharding<TShardingDbContext>, contextLifetime,
|
||||
optionsLifetime);
|
||||
services.TryAddSingleton<IShardingRuntimeContextManager, ShardingRuntimeContextManager>();
|
||||
return services.AddShardingConfigure<TShardingDbContext>();
|
||||
}
|
||||
|
||||
public static ShardingCoreConfigBuilder<TShardingDbContext> AddShardingConfigure<TShardingDbContext>(
|
||||
this IServiceCollection services)
|
||||
where TShardingDbContext : DbContext, IShardingDbContext
|
||||
|
@ -131,20 +116,6 @@ namespace ShardingCore
|
|||
dbContextOptionsBuilder.UseDefaultSharding<TShardingDbContext>(shardingRuntimeContext);
|
||||
}
|
||||
|
||||
public static void UseMutliDefaultSharding<TShardingDbContext>(IServiceProvider serviceProvider,
|
||||
DbContextOptionsBuilder dbContextOptionsBuilder) where TShardingDbContext : DbContext, IShardingDbContext
|
||||
{
|
||||
var shardingRuntimeContextManager = serviceProvider.GetRequiredService<IShardingRuntimeContextManager>();
|
||||
var shardingRuntimeContext = shardingRuntimeContextManager.TryGet(typeof(TShardingDbContext));
|
||||
if (shardingRuntimeContext == null)
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
$"cant get multi sharding runtime context:[{typeof(TShardingDbContext)}]");
|
||||
}
|
||||
|
||||
dbContextOptionsBuilder.UseDefaultSharding<TShardingDbContext>(shardingRuntimeContext);
|
||||
}
|
||||
|
||||
public static DbContextOptionsBuilder UseDefaultSharding<TShardingDbContext>(this DbContextOptionsBuilder dbContextOptionsBuilder,
|
||||
IShardingRuntimeContext shardingRuntimeContext) where TShardingDbContext : DbContext, IShardingDbContext
|
||||
{
|
||||
|
@ -164,6 +135,7 @@ namespace ShardingCore
|
|||
internal static IServiceCollection AddInternalShardingCore<TShardingDbContext>(this IServiceCollection services)
|
||||
where TShardingDbContext : DbContext, IShardingDbContext
|
||||
{
|
||||
services.TryAddSingleton<IDbContextTypeAware>(sp=>new DbContextTypeAware(typeof(TShardingDbContext)));
|
||||
services.TryAddSingleton<IShardingInitializer, ShardingInitializer>();
|
||||
services.TryAddSingleton<IShardingBootstrapper, ShardingBootstrapper>();
|
||||
services.TryAddSingleton<IDataSourceInitializer, DataSourceInitializer>();
|
||||
|
|
|
@ -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<string> GetExistTables(IShardingDbContext shardingDbContext, string dataSourceName)
|
||||
{
|
||||
return new HashSet<string>();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue