diff --git a/samples/Sample.BulkConsole/Program.cs b/samples/Sample.BulkConsole/Program.cs index 861e4b68..b1c000e2 100644 --- a/samples/Sample.BulkConsole/Program.cs +++ b/samples/Sample.BulkConsole/Program.cs @@ -89,7 +89,16 @@ namespace Sample.BulkConsole var b = DateTime.Now.Date.AddDays(-3); var queryable = myShardingDbContext.Set().Select(o=>new {Id=o.Id,OrderNo=o.OrderNo, CreateTime =o.CreateTime });//.Where(o => o.CreateTime >= b); - + var dateTime = DateTime.Now; + var dbContexts = myShardingDbContext.BulkShardingTableExpression(o=>o.CreateTime<=dateTime); + using (var dbContextTransaction = myShardingDbContext.Database.BeginTransaction()) + { + foreach (var dbContext in dbContexts) + { + dbContext.Set().Where(o=>o.CreateTime<=dateTime).BatchUpdate(a => new Order { OrderNo = "12345" }); + } + dbContextTransaction.Commit(); + } var startNew1 = Stopwatch.StartNew(); diff --git a/src/ShardingCore/Core/RuntimeContexts/IShardingRuntimeContext`1.cs b/src/ShardingCore/Core/RuntimeContexts/IShardingRuntimeContext`1.cs new file mode 100644 index 00000000..84afd23a --- /dev/null +++ b/src/ShardingCore/Core/RuntimeContexts/IShardingRuntimeContext`1.cs @@ -0,0 +1,8 @@ +using ShardingCore.Sharding.Abstractions; + +namespace ShardingCore.Core.RuntimeContexts; + +public interface IShardingRuntimeContext:IShardingRuntimeContext where TDbContext:IShardingDbContext +{ + +} \ No newline at end of file diff --git a/src/ShardingCore/Core/RuntimeContexts/ShardingRuntimeContext.cs b/src/ShardingCore/Core/RuntimeContexts/ShardingRuntimeContext.cs index 75e32072..e10c455d 100644 --- a/src/ShardingCore/Core/RuntimeContexts/ShardingRuntimeContext.cs +++ b/src/ShardingCore/Core/RuntimeContexts/ShardingRuntimeContext.cs @@ -31,7 +31,7 @@ using ShardingCore.TableCreator; namespace ShardingCore.Core.RuntimeContexts { - public sealed class ShardingRuntimeContext : IShardingRuntimeContext + public sealed class ShardingRuntimeContext : IShardingRuntimeContext where TDbContext:IShardingDbContext { private bool isInited = false; private object INIT_LOCK = new object(); @@ -42,12 +42,9 @@ namespace ShardingCore.Core.RuntimeContexts private IServiceCollection _serviceMap = new ServiceCollection(); private IServiceProvider _serviceProvider; - public Type DbContextType { get; } - // private ILoggerFactory _applicationLoggerFactory; - public ShardingRuntimeContext(Type dbContextType) - { - DbContextType = dbContextType; - } + + public Type DbContextType => typeof(TDbContext); + public void AddServiceConfig(Action configure) { diff --git a/src/ShardingCore/Core/ShardingConfigurations/ConfigBuilders/ShardingCoreConfigBuilder.cs b/src/ShardingCore/Core/ShardingConfigurations/ConfigBuilders/ShardingCoreConfigBuilder.cs index 149496f6..578ab1df 100644 --- a/src/ShardingCore/Core/ShardingConfigurations/ConfigBuilders/ShardingCoreConfigBuilder.cs +++ b/src/ShardingCore/Core/ShardingConfigurations/ConfigBuilders/ShardingCoreConfigBuilder.cs @@ -79,11 +79,12 @@ namespace ShardingCore.Core.ShardingConfigurations.ConfigBuilders [Obsolete("plz use AddShardingCore")] public void EnsureConfig() { - _services.AddSingleton(sp => _shardingRuntimeBuilder.Build(sp)); + AddShardingCore(); } public void AddShardingCore() { - _services.AddSingleton(sp => _shardingRuntimeBuilder.Build(sp)); + _services.AddSingleton>(sp => _shardingRuntimeBuilder.Build(sp)); + _services.AddSingleton(sp => sp.GetService>()); } } diff --git a/src/ShardingCore/ShardingCoreExtension.cs b/src/ShardingCore/ShardingCoreExtension.cs index fddf34f3..e6c36e38 100644 --- a/src/ShardingCore/ShardingCoreExtension.cs +++ b/src/ShardingCore/ShardingCoreExtension.cs @@ -106,14 +106,14 @@ namespace ShardingCore public static void UseDefaultSharding(this DbContextOptionsBuilder dbContextOptionsBuilder, IServiceProvider serviceProvider) where TShardingDbContext : DbContext, IShardingDbContext { - var shardingRuntimeContext = serviceProvider.GetRequiredService(); + var shardingRuntimeContext = serviceProvider.GetRequiredService>(); dbContextOptionsBuilder.UseDefaultSharding(shardingRuntimeContext); } public static void UseDefaultSharding(IServiceProvider serviceProvider, DbContextOptionsBuilder dbContextOptionsBuilder) where TShardingDbContext : DbContext, IShardingDbContext { - var shardingRuntimeContext = serviceProvider.GetRequiredService(); + var shardingRuntimeContext = serviceProvider.GetRequiredService>(); dbContextOptionsBuilder.UseDefaultSharding(shardingRuntimeContext); } diff --git a/src/ShardingCore/ShardingRuntimeBuilder.cs b/src/ShardingCore/ShardingRuntimeBuilder.cs index 055f63af..4d7beb9c 100644 --- a/src/ShardingCore/ShardingRuntimeBuilder.cs +++ b/src/ShardingCore/ShardingRuntimeBuilder.cs @@ -71,18 +71,18 @@ namespace ShardingCore return this; } - public IShardingRuntimeContext Build() + public IShardingRuntimeContext Build() { return Build(null); } - public IShardingRuntimeContext Build(IServiceProvider appServiceProvider) + public IShardingRuntimeContext Build(IServiceProvider appServiceProvider) { return Build(appServiceProvider, appServiceProvider?.GetService()); } - public IShardingRuntimeContext Build(IServiceProvider appServiceProvider, ILoggerFactory loggerFactory) + public IShardingRuntimeContext Build(IServiceProvider appServiceProvider, ILoggerFactory loggerFactory) { - var shardingRuntimeContext = new ShardingRuntimeContext(typeof(TShardingDbContext)); + var shardingRuntimeContext = new ShardingRuntimeContext(); shardingRuntimeContext.AddServiceConfig(services => { // services.AddSingleton(sp => new DbContextTypeCollector()); diff --git a/test/ShardingCore.Test/ShardingTest.cs b/test/ShardingCore.Test/ShardingTest.cs index 4a2c89bf..5a5b3cd4 100644 --- a/test/ShardingCore.Test/ShardingTest.cs +++ b/test/ShardingCore.Test/ShardingTest.cs @@ -174,12 +174,12 @@ namespace ShardingCore.Test await _virtualDbContext.AddRangeAsync(logDays); var bulkShardingExpression = _virtualDbContext.BulkShardingExpression(o => new[] { "A", "B" }.Contains(o.Area)); + Assert.Equal(2, bulkShardingExpression.Count); Assert.True(bulkShardingExpression.ContainsKey("A")); Assert.True(bulkShardingExpression.ContainsKey("B")); var bulkShardingTableExpression = _virtualDbContext.BulkShardingTableExpression(o => o.Id == Guid.NewGuid().ToString()); - Assert.Equal(1, bulkShardingTableExpression.Count()); var noShardingExpression = _virtualDbContext.BulkShardingExpression(o => o.Id == "123");