From b73127b11a3ec35288e8934b3f9b67e48983f82f Mon Sep 17 00:00:00 2001 From: xuejiaming <326308290@qq.com> Date: Thu, 8 Apr 2021 20:57:49 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E9=83=A8=E5=88=86=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=E4=BF=AE=E6=94=B9=E9=83=A8=E5=88=86demo=E7=9A=84?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=E6=AD=A3=E7=A1=AE=E6=80=A7=EF=BC=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 8 +- ShardingCore.sln | 11 +- nuget-publish.bat | 6 +- .../DbContexts/DefaultTableDbContext.cs | 2 +- .../DbContexts/DefaultTableDbContext.cs | 2 +- .../Jobs/AutoCreateTableByDay.cs | 14 +- .../ShardingMySqlParallelDbContextFactory.cs | 2 +- ...ardingSqlServerParallelDbContextFactory.cs | 2 +- .../Abstractions/IDbContextCreateFilter.cs | 2 +- .../DbContexts/IShardingDbContextFactory.cs | 4 +- .../DbContexts/ShardingDbContextFactory.cs | 18 +-- .../VirtualDbContexts/VirtualDbContext.cs | 3 +- src/ShardingCore/ShardingBootstrapper.cs | 4 +- .../TableCreator/ShardingTableCreator.cs | 2 +- .../ShardingCoreTestBatch/DefaultDbContext.cs | 29 +++++ .../Domain/Entities/SysUserMod.cs | 29 +++++ .../Domain/Maps/SysUserModMap.cs | 23 ++++ .../ShardingCoreTestBatch.csproj | 30 +++++ test/ShardingCoreTestBatch/ShardingTest.cs | 58 +++++++++ .../Shardings/SysUserModVirtualTableRoute.cs | 19 +++ test/ShardingCoreTestBatch/Startup.cs | 120 ++++++++++++++++++ 21 files changed, 350 insertions(+), 38 deletions(-) create mode 100644 test/ShardingCoreTestBatch/DefaultDbContext.cs create mode 100644 test/ShardingCoreTestBatch/Domain/Entities/SysUserMod.cs create mode 100644 test/ShardingCoreTestBatch/Domain/Maps/SysUserModMap.cs create mode 100644 test/ShardingCoreTestBatch/ShardingCoreTestBatch.csproj create mode 100644 test/ShardingCoreTestBatch/ShardingTest.cs create mode 100644 test/ShardingCoreTestBatch/Shardings/SysUserModVirtualTableRoute.cs create mode 100644 test/ShardingCoreTestBatch/Startup.cs diff --git a/README.md b/README.md index b1affd23..3aed61ed 100644 --- a/README.md +++ b/README.md @@ -8,9 +8,9 @@ Release | EF Core | .NET Standard | .NET (Core) | Sql Server | [Pomelo.EntityFrameworkCore.MySql](https://github.com/PomeloFoundation/Pomelo.EntityFrameworkCore.MySql) --- | --- | --- | --- | --- | --- -[5.1.x.x](https://www.nuget.org/packages/ShardingCore/5.1.0.3) | >= 5.0.x | 2.1 | 3.0+ | >= 2012 | 5.0.0-alpha.2 -[3.1.x.x](https://www.nuget.org/packages/ShardingCore/3.1.0.3) | 3.1.10 | 2.0 | 2.0+ | >= 2012 | 3.2.4 -[2.1.x.x](https://www.nuget.org/packages/ShardingCore/2.1.0.3) | 2.2.6 | 2.0 | 2.0+ | >= 2008 | 2.2.6 +[5.1.x.x](https://www.nuget.org/packages/ShardingCore/5.1.0.9) | >= 5.0.x | 2.1 | 3.0+ | >= 2012 | 5.0.0-alpha.2 +[3.1.x.x](https://www.nuget.org/packages/ShardingCore/3.1.0.9) | 3.1.10 | 2.0 | 2.0+ | >= 2012 | 3.2.4 +[2.1.x.x](https://www.nuget.org/packages/ShardingCore/2.1.0.9) | 2.2.6 | 2.0 | 2.0+ | >= 2008 | 2.2.6 - [开始](#开始) @@ -80,7 +80,7 @@ Release | EF Core | .NET Standard | .NET (Core) | Sql Server | [Pomelo.EntityFr ## 安装 ```xml - + ``` ## 配置 diff --git a/ShardingCore.sln b/ShardingCore.sln index 2a76d07f..9f646238 100644 --- a/ShardingCore.sln +++ b/ShardingCore.sln @@ -41,9 +41,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Samples.AutoByDate.SqlServe EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sample.MySql", "samples\Sample.MySql\Sample.MySql.csproj", "{90675788-D5C3-415A-9C18-FF159A75B4D5}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sample.SqlServer3x", "samples\Sample.SqlServer3x\Sample.SqlServer3x.csproj", "{447D5357-F095-45DE-9DA5-2D9997237366}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sample.SqlServer3x", "samples\Sample.SqlServer3x\Sample.SqlServer3x.csproj", "{447D5357-F095-45DE-9DA5-2D9997237366}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ShardingCoreTestSqlServer3x", "test\ShardingCoreTestSqlServer3x\ShardingCoreTestSqlServer3x.csproj", "{1CE858B8-56D8-4009-BF46-EE0F79F259D1}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ShardingCoreTestSqlServer3x", "test\ShardingCoreTestSqlServer3x\ShardingCoreTestSqlServer3x.csproj", "{1CE858B8-56D8-4009-BF46-EE0F79F259D1}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ShardingCoreTestBatch", "test\ShardingCoreTestBatch\ShardingCoreTestBatch.csproj", "{54BA9F11-96CD-47DF-97FB-0BC83D2F7081}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -115,6 +117,10 @@ Global {1CE858B8-56D8-4009-BF46-EE0F79F259D1}.Debug|Any CPU.Build.0 = Debug|Any CPU {1CE858B8-56D8-4009-BF46-EE0F79F259D1}.Release|Any CPU.ActiveCfg = Release|Any CPU {1CE858B8-56D8-4009-BF46-EE0F79F259D1}.Release|Any CPU.Build.0 = Release|Any CPU + {54BA9F11-96CD-47DF-97FB-0BC83D2F7081}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {54BA9F11-96CD-47DF-97FB-0BC83D2F7081}.Debug|Any CPU.Build.0 = Debug|Any CPU + {54BA9F11-96CD-47DF-97FB-0BC83D2F7081}.Release|Any CPU.ActiveCfg = Release|Any CPU + {54BA9F11-96CD-47DF-97FB-0BC83D2F7081}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -136,6 +142,7 @@ Global {90675788-D5C3-415A-9C18-FF159A75B4D5} = {EDF8869A-C1E1-491B-BC9F-4A33F4DE1C73} {447D5357-F095-45DE-9DA5-2D9997237366} = {EDF8869A-C1E1-491B-BC9F-4A33F4DE1C73} {1CE858B8-56D8-4009-BF46-EE0F79F259D1} = {CC2C88C0-65F2-445D-BE78-973B840FE281} + {54BA9F11-96CD-47DF-97FB-0BC83D2F7081} = {CC2C88C0-65F2-445D-BE78-973B840FE281} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {8C07A667-E8B4-43C7-8053-721584BAD291} diff --git a/nuget-publish.bat b/nuget-publish.bat index 6e0ed12a..e45275e8 100644 --- a/nuget-publish.bat +++ b/nuget-publish.bat @@ -1,8 +1,8 @@ :start ::定义版本 -set EFCORE2=2.1.0.8 -set EFCORE3=3.1.0.8 -set EFCORE5=5.1.0.8 +set EFCORE2=2.1.0.10 +set EFCORE3=3.1.0.10 +set EFCORE5=5.1.0.10 ::删除所有bin与obj下的文件 @echo off diff --git a/samples/Sample.MySql/DbContexts/DefaultTableDbContext.cs b/samples/Sample.MySql/DbContexts/DefaultTableDbContext.cs index 5251d146..ac665902 100644 --- a/samples/Sample.MySql/DbContexts/DefaultTableDbContext.cs +++ b/samples/Sample.MySql/DbContexts/DefaultTableDbContext.cs @@ -6,7 +6,7 @@ namespace Sample.MySql.DbContexts { public class DefaultTableDbContext: DbContext,IShardingTableDbContext { - public DefaultTableDbContext(ShardingDbContextOptions shardingDbContextOptions):base(shardingDbContextOptions.DbContextOptions) + public DefaultTableDbContext(DbContextOptions options) :base(options) { } diff --git a/samples/Sample.SqlServer/DbContexts/DefaultTableDbContext.cs b/samples/Sample.SqlServer/DbContexts/DefaultTableDbContext.cs index 28210063..a7198824 100644 --- a/samples/Sample.SqlServer/DbContexts/DefaultTableDbContext.cs +++ b/samples/Sample.SqlServer/DbContexts/DefaultTableDbContext.cs @@ -10,7 +10,7 @@ namespace Sample.SqlServer.DbContexts { public class DefaultTableDbContext: DbContext,IShardingTableDbContext { - public DefaultTableDbContext(ShardingDbContextOptions shardingDbContextOptions):base(shardingDbContextOptions.DbContextOptions) + public DefaultTableDbContext(DbContextOptions options) :base(options) { } diff --git a/samples/Samples.AutoByDate.SqlServer/Jobs/AutoCreateTableByDay.cs b/samples/Samples.AutoByDate.SqlServer/Jobs/AutoCreateTableByDay.cs index fd947bfc..8fdca22e 100644 --- a/samples/Samples.AutoByDate.SqlServer/Jobs/AutoCreateTableByDay.cs +++ b/samples/Samples.AutoByDate.SqlServer/Jobs/AutoCreateTableByDay.cs @@ -8,12 +8,12 @@ using ShardingCore.TableCreator; namespace Samples.AutoByDate.SqlServer.Jobs { -/* -* @Author: xjm -* @Description: -* @Date: Tuesday, 02 February 2021 17:24:17 -* @Email: 326308290@qq.com -*/ + /* + * @Author: xjm + * @Description: + * @Date: Tuesday, 02 February 2021 17:24:17 + * @Email: 326308290@qq.com + */ //public class AutoCreateTableByDay : IJob //{ // /// @@ -34,8 +34,8 @@ namespace Samples.AutoByDate.SqlServer.Jobs // var tail = virtualTable.GetVirtualRoute().ShardingKeyToTail(now); // try // { - // tableCreator.CreateTable(tail); // virtualTableManager.AddPhysicTable(virtualTable, new DefaultPhysicTable(virtualTable, tail)); + // tableCreator.CreateTable(tail); // } // catch (Exception e) // { diff --git a/src/ShardingCore.MySql/ShardingMySqlParallelDbContextFactory.cs b/src/ShardingCore.MySql/ShardingMySqlParallelDbContextFactory.cs index 9c6084fc..af263148 100644 --- a/src/ShardingCore.MySql/ShardingMySqlParallelDbContextFactory.cs +++ b/src/ShardingCore.MySql/ShardingMySqlParallelDbContextFactory.cs @@ -35,7 +35,7 @@ namespace ShardingCore.MySql { var shardingConfigEntry = _shardingCoreOptions.GetShardingConfig(connectKey); var shardingDbContextOptions = new ShardingDbContextOptions(CreateOptions(connectKey,shardingConfigEntry.ConnectionString), tail); - return _shardingDbContextFactory.Create(connectKey, shardingDbContextOptions,null); + return _shardingDbContextFactory.Create(connectKey, shardingDbContextOptions); } private DbContextOptions CreateOptions(string connectKey, string connectionString) diff --git a/src/ShardingCore.SqlServer/ShardingSqlServerParallelDbContextFactory.cs b/src/ShardingCore.SqlServer/ShardingSqlServerParallelDbContextFactory.cs index 94b88f4f..0a5adc8d 100644 --- a/src/ShardingCore.SqlServer/ShardingSqlServerParallelDbContextFactory.cs +++ b/src/ShardingCore.SqlServer/ShardingSqlServerParallelDbContextFactory.cs @@ -36,7 +36,7 @@ namespace ShardingCore.SqlServer { var shardingConfigEntry = _shardingCoreOptions.GetShardingConfig(connectKey); var shardingDbContextOptions = new ShardingDbContextOptions(CreateOptions(connectKey, shardingConfigEntry.ConnectionString), tail); - return _shardingDbContextFactory.Create(connectKey, shardingDbContextOptions,null); + return _shardingDbContextFactory.Create(connectKey, shardingDbContextOptions); } private DbContextOptions CreateOptions(string connectKey, string connectString) diff --git a/src/ShardingCore/DbContexts/Abstractions/IDbContextCreateFilter.cs b/src/ShardingCore/DbContexts/Abstractions/IDbContextCreateFilter.cs index b51a0dc8..e284cdc3 100644 --- a/src/ShardingCore/DbContexts/Abstractions/IDbContextCreateFilter.cs +++ b/src/ShardingCore/DbContexts/Abstractions/IDbContextCreateFilter.cs @@ -19,6 +19,6 @@ namespace ShardingCore.DbContexts.Abstractions /// /// /// - void CreateAfter(DbContext dbContext, IServiceProvider s); + void CreateAfter(DbContext dbContext); } } diff --git a/src/ShardingCore/DbContexts/IShardingDbContextFactory.cs b/src/ShardingCore/DbContexts/IShardingDbContextFactory.cs index 010e5243..efd2519a 100644 --- a/src/ShardingCore/DbContexts/IShardingDbContextFactory.cs +++ b/src/ShardingCore/DbContexts/IShardingDbContextFactory.cs @@ -13,7 +13,7 @@ namespace ShardingCore.DbContexts */ public interface IShardingDbContextFactory { - DbContext Create(string connectKey,ShardingDbContextOptions shardingDbContextOptions,IServiceProvider serviceProvider); - DbContext Create(string connectKey,string tail, IServiceProvider serviceProvider); + DbContext Create(string connectKey,ShardingDbContextOptions shardingDbContextOptions); + DbContext Create(string connectKey,string tail, IDbContextOptionsProvider dbContextOptionsProvider); } } \ No newline at end of file diff --git a/src/ShardingCore/DbContexts/ShardingDbContextFactory.cs b/src/ShardingCore/DbContexts/ShardingDbContextFactory.cs index 7c1d2747..49326788 100644 --- a/src/ShardingCore/DbContexts/ShardingDbContextFactory.cs +++ b/src/ShardingCore/DbContexts/ShardingDbContextFactory.cs @@ -28,7 +28,7 @@ namespace ShardingCore.DbContexts _shardingTableScopeFactory = shardingTableScopeFactory; _dbContextCreateFilterManager = dbContextCreateFilterManager; } - public DbContext Create(string connectKey, ShardingDbContextOptions shardingDbContextOptions,IServiceProvider serviceProvider) + public DbContext Create(string connectKey, ShardingDbContextOptions shardingDbContextOptions) { var shardingConfigEntry = _shardingCoreOptions.GetShardingConfig(connectKey); @@ -48,15 +48,12 @@ namespace ShardingCore.DbContexts shardingTableDbContext.ModelChangeKey = modelChangeKey; } - if (serviceProvider != null) + var filters = _dbContextCreateFilterManager.GetFilters(); + if (filters.Any()) { - var filters = _dbContextCreateFilterManager.GetFilters(); - if (filters.Any()) + foreach (var dbContextCreateFilter in filters) { - foreach (var dbContextCreateFilter in filters) - { - dbContextCreateFilter.CreateAfter(dbContext, serviceProvider); - } + dbContextCreateFilter.CreateAfter(dbContext); } } var dbContextModel = dbContext.Model; @@ -64,12 +61,11 @@ namespace ShardingCore.DbContexts } } - public DbContext Create(string connectKey, string tail,IServiceProvider serviceProvider) + public DbContext Create(string connectKey, string tail, IDbContextOptionsProvider dbContextOptionsProvider) { - var dbContextOptionsProvider = serviceProvider.GetService(); var shardingDbContextOptions = new ShardingDbContextOptions(dbContextOptionsProvider.GetDbContextOptions(connectKey), tail); - return Create(connectKey,shardingDbContextOptions,serviceProvider); + return Create(connectKey,shardingDbContextOptions); } } } \ No newline at end of file diff --git a/src/ShardingCore/DbContexts/VirtualDbContexts/VirtualDbContext.cs b/src/ShardingCore/DbContexts/VirtualDbContexts/VirtualDbContext.cs index 384dcb5b..958b3971 100644 --- a/src/ShardingCore/DbContexts/VirtualDbContexts/VirtualDbContext.cs +++ b/src/ShardingCore/DbContexts/VirtualDbContexts/VirtualDbContext.cs @@ -5,6 +5,7 @@ using System.Linq; using System.Linq.Expressions; using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.DependencyInjection; using ShardingCore.Core; using ShardingCore.Core.VirtualDataSources; using ShardingCore.Core.VirtualRoutes; @@ -382,7 +383,7 @@ namespace ShardingCore.DbContexts.VirtualDbContexts } if(!dbContexts.TryGetValue(tail,out var dbContext)) { - dbContext = _shardingDbContextFactory.Create(connectKey, tail == EMPTY_SHARDING_TAIL_ID ? string.Empty : tail, _serviceProvider); + dbContext = _shardingDbContextFactory.Create(connectKey, tail == EMPTY_SHARDING_TAIL_ID ? string.Empty : tail, _serviceProvider.GetService()); dbContexts.TryAdd(tail, dbContext); } diff --git a/src/ShardingCore/ShardingBootstrapper.cs b/src/ShardingCore/ShardingBootstrapper.cs index e903a2bb..7007228f 100644 --- a/src/ShardingCore/ShardingBootstrapper.cs +++ b/src/ShardingCore/ShardingBootstrapper.cs @@ -83,7 +83,7 @@ namespace ShardingCore var dbContextOptionsProvider = scope.ServiceProvider.GetService(); using var context = _shardingDbContextFactory.Create(connectKey, new ShardingDbContextOptions(dbContextOptionsProvider.GetDbContextOptions(connectKey), - string.Empty),scope.ServiceProvider); + string.Empty)); foreach (var entity in context.Model.GetEntityTypes()) { _virtualDataSourceManager.AddConnectEntities(connectKey, entity.ClrType); @@ -186,7 +186,7 @@ namespace ShardingCore var dbContextOptionsProvider = scope.ServiceProvider.GetService(); using var context = _shardingDbContextFactory.Create(shardingConfig.ConnectKey, new ShardingDbContextOptions( - dbContextOptionsProvider.GetDbContextOptions(shardingConfig.ConnectKey), string.Empty),scope.ServiceProvider); + dbContextOptionsProvider.GetDbContextOptions(shardingConfig.ConnectKey), string.Empty)); var modelCacheSyncObject = context.GetModelCacheSyncObject(); lock (modelCacheSyncObject) diff --git a/src/ShardingCore/TableCreator/ShardingTableCreator.cs b/src/ShardingCore/TableCreator/ShardingTableCreator.cs index 493ae79d..7418604f 100644 --- a/src/ShardingCore/TableCreator/ShardingTableCreator.cs +++ b/src/ShardingCore/TableCreator/ShardingTableCreator.cs @@ -56,7 +56,7 @@ namespace ShardingCore.TableCreator var dbContextOptionsProvider = serviceScope.ServiceProvider.GetService(); var virtualTable = _virtualTableManager.GetVirtualTable(connectKey,shardingEntityType); - using (var dbContext = _shardingDbContextFactory.Create(connectKey,new ShardingDbContextOptions(dbContextOptionsProvider.GetDbContextOptions(connectKey), tail),serviceScope.ServiceProvider)) + using (var dbContext = _shardingDbContextFactory.Create(connectKey,new ShardingDbContextOptions(dbContextOptionsProvider.GetDbContextOptions(connectKey), tail))) { var modelCacheSyncObject = dbContext.GetModelCacheSyncObject(); diff --git a/test/ShardingCoreTestBatch/DefaultDbContext.cs b/test/ShardingCoreTestBatch/DefaultDbContext.cs new file mode 100644 index 00000000..64c38568 --- /dev/null +++ b/test/ShardingCoreTestBatch/DefaultDbContext.cs @@ -0,0 +1,29 @@ +using Microsoft.EntityFrameworkCore; +using ShardingCore.DbContexts.ShardingDbContexts; +using ShardingCoreTestBatch.Domain.Maps; + +namespace ShardingCoreTestBatch +{ + /* + * @Author: xjm + * @Description: + * @Date: 2021/3/31 15:28:11 + * @Ver: 1.0 + * @Email: 326308290@qq.com + */ + public class DefaultDbContext : DbContext, IShardingTableDbContext + { + public DefaultDbContext(DbContextOptions options) : base(options) + { + + } + + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + base.OnModelCreating(modelBuilder); + modelBuilder.ApplyConfiguration(new SysUserModMap()); + } + + public string ModelChangeKey { get; set; } + } +} diff --git a/test/ShardingCoreTestBatch/Domain/Entities/SysUserMod.cs b/test/ShardingCoreTestBatch/Domain/Entities/SysUserMod.cs new file mode 100644 index 00000000..aeb7b784 --- /dev/null +++ b/test/ShardingCoreTestBatch/Domain/Entities/SysUserMod.cs @@ -0,0 +1,29 @@ +using ShardingCore.Core; + +namespace ShardingCoreTestBatch.Domain.Entities +{ +/* +* @Author: xjm +* @Description: +* @Date: Thursday, 14 January 2021 15:36:43 +* @Email: 326308290@qq.com +*/ + public class SysUserMod:IShardingTable + { + /// + /// 用户Id用于分表 + /// + [ShardingTableKey(TailPrefix = "_")] + public string Id { get; set; } + /// + /// 用户名称 + /// + public string Name { get; set; } + /// + /// 用户姓名 + /// + public int Age { get; set; } + public int AgeGroup { get; set; } + + } +} \ No newline at end of file diff --git a/test/ShardingCoreTestBatch/Domain/Maps/SysUserModMap.cs b/test/ShardingCoreTestBatch/Domain/Maps/SysUserModMap.cs new file mode 100644 index 00000000..81f03f6b --- /dev/null +++ b/test/ShardingCoreTestBatch/Domain/Maps/SysUserModMap.cs @@ -0,0 +1,23 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Metadata.Builders; +using ShardingCoreTestBatch.Domain.Entities; + +namespace ShardingCoreTestBatch.Domain.Maps +{ +/* +* @Author: xjm +* @Description: +* @Date: Thursday, 14 January 2021 15:37:33 +* @Email: 326308290@qq.com +*/ + public class SysUserModMap:IEntityTypeConfiguration + { + public void Configure(EntityTypeBuilder builder) + { + builder.HasKey(o => o.Id); + builder.Property(o => o.Id).IsRequired().HasMaxLength(128); + builder.Property(o => o.Name).HasMaxLength(128); + builder.ToTable(nameof(SysUserMod)); + } + } +} \ No newline at end of file diff --git a/test/ShardingCoreTestBatch/ShardingCoreTestBatch.csproj b/test/ShardingCoreTestBatch/ShardingCoreTestBatch.csproj new file mode 100644 index 00000000..0cf9a074 --- /dev/null +++ b/test/ShardingCoreTestBatch/ShardingCoreTestBatch.csproj @@ -0,0 +1,30 @@ + + + + net5.0 + 9.0 + + + + + + + + + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + diff --git a/test/ShardingCoreTestBatch/ShardingTest.cs b/test/ShardingCoreTestBatch/ShardingTest.cs new file mode 100644 index 00000000..36df9015 --- /dev/null +++ b/test/ShardingCoreTestBatch/ShardingTest.cs @@ -0,0 +1,58 @@ +using System; +using System.Diagnostics; +using System.Linq; +using System.Threading.Tasks; +using ShardingCore.Core.VirtualRoutes.TableRoutes.RoutingRuleEngine; +using ShardingCore.DbContexts.VirtualDbContexts; +using ShardingCore.Extensions; +using ShardingCoreTestBatch.Domain.Entities; +using Xunit; + +namespace ShardingCoreTestBatch +{ +/* +* @Author: xjm +* @Description: +* @Date: Friday, 15 January 2021 17:22:10 +* @Email: 326308290@qq.com +*/ + public class ShardingTest + { + private readonly IVirtualDbContext _virtualDbContext; + private readonly IRoutingRuleEngineFactory _routingRuleEngineFactory; + + public ShardingTest(IVirtualDbContext virtualDbContext,IRoutingRuleEngineFactory routingRuleEngineFactory) + { + _virtualDbContext = virtualDbContext; + _routingRuleEngineFactory = routingRuleEngineFactory; + } + + //[Fact] + //public async Task Route_TEST() + //{ + // var queryable1 = _virtualDbContext.Set().Where(o=>o.Id=="339"); + // var routeResults1 = _routingRuleEngineFactory.Route(queryable1); + // Assert.Equal(1,routeResults1.Count()); + // Assert.Equal(1,routeResults1.FirstOrDefault().ReplaceTables.Count()); + // Assert.Equal("0",routeResults1.FirstOrDefault().ReplaceTables.FirstOrDefault().Tail); + // Assert.Equal(nameof(SysUserMod),routeResults1.FirstOrDefault().ReplaceTables.FirstOrDefault().OriginalName); + // var ids = new[] {"339", "124","142"}; + // var queryable2= _virtualDbContext.Set().Where(o=>ids.Contains(o.Id)); + // var routeResult2s = _routingRuleEngineFactory.Route(queryable2); + // Assert.Equal(2,routeResult2s.Count()); + // Assert.Equal(1,routeResult2s.FirstOrDefault().ReplaceTables.Count()); + // Assert.Equal(2,routeResult2s.SelectMany(o=>o.ReplaceTables).Count()); + // Assert.Equal(true,routeResult2s.SelectMany(o=>o.ReplaceTables).All(o=>new[]{"0","1"}.Contains(o.Tail))); + //} + [Fact] + public async Task ToList_All_Test() + { + var startNew = Stopwatch.StartNew(); startNew.Start(); + var mods = await _virtualDbContext.Set().Where(o=>o.Age== 7601935).ToShardingListAsync(); + startNew.Stop(); + var x = startNew.ElapsedMilliseconds; + Console.WriteLine(mods.Count); + } + + } +} \ No newline at end of file diff --git a/test/ShardingCoreTestBatch/Shardings/SysUserModVirtualTableRoute.cs b/test/ShardingCoreTestBatch/Shardings/SysUserModVirtualTableRoute.cs new file mode 100644 index 00000000..3100e056 --- /dev/null +++ b/test/ShardingCoreTestBatch/Shardings/SysUserModVirtualTableRoute.cs @@ -0,0 +1,19 @@ +using ShardingCore.VirtualRoutes.Mods; +using ShardingCoreTestBatch.Domain.Entities; + +namespace ShardingCoreTestBatch.Shardings +{ +/* +* @Author: xjm +* @Description: +* @Date: Thursday, 14 January 2021 15:39:27 +* @Email: 326308290@qq.com +*/ + public class SysUserModVirtualTableRoute : AbstractSimpleShardingModKeyStringVirtualTableRoute + { + public SysUserModVirtualTableRoute() : base(2,15) + { + } + + } +} \ No newline at end of file diff --git a/test/ShardingCoreTestBatch/Startup.cs b/test/ShardingCoreTestBatch/Startup.cs new file mode 100644 index 00000000..00437ecc --- /dev/null +++ b/test/ShardingCoreTestBatch/Startup.cs @@ -0,0 +1,120 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text.RegularExpressions; +using System.Threading.Tasks; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using ShardingCore; +using ShardingCore.DbContexts.VirtualDbContexts; +using ShardingCore.Extensions; +using ShardingCore.SqlServer; +using ShardingCoreTestBatch.Domain.Entities; +using ShardingCoreTestBatch.Shardings; + +#if EFCORE5SQLSERVER +using ShardingCore.SqlServer; +#endif +#if EFCORE5MYSQL +using ShardingCore.MySql; +#endif + +namespace ShardingCoreTestBatch +{ +/* +* @Author: xjm +* @Description: +* @Date: Friday, 15 January 2021 15:37:46 +* @Email: 326308290@qq.com +*/ + public class Startup + { + // // 自定义 host 构建 + //public void ConfigureHost(IHostBuilder hostBuilder) + //{ + // hostBuilder + // .ConfigureAppConfiguration(builder => + // { + // builder.AddJsonFile("Configs/DbConfig.json"); + // }); + //} + + // 支持的形式: + // ConfigureServices(IServiceCollection services) + // ConfigureServices(IServiceCollection services, HostBuilderContext hostBuilderContext) + // ConfigureServices(HostBuilderContext hostBuilderContext, IServiceCollection services) + public void ConfigureServices(IServiceCollection services, HostBuilderContext hostBuilderContext) + { + + services.AddShardingSqlServer(o => + { + o.EnsureCreatedWithOutShardingTable = true; + o.CreateShardingTableOnStart = true; + o.AddShardingDbContextWithShardingTable("conn1", "Data Source=localhost;Initial Catalog=ShardingCoreDBBatch;Integrated Security=True", dbConfig => + { + dbConfig.AddShardingTableRoute(); + }); + //o.AddDataSourceVirtualRoute<>(); + + }); + //services.AddShardingSqlServer(o => + //{ + // o.ConnectionString = hostBuilderContext.Configuration.GetSection("SqlServer")["ConnectionString"]; + // o.AddSharding(); + // o.AddSharding(); + // o.UseShardingCoreConfig((provider, config) => + // { + // config.EnsureCreated = true; + // config.CreateShardingTableOnStart = true; + // }); + //}); + } + + // 可以添加要用到的方法参数,会自动从注册的服务中获取服务实例,类似于 asp.net core 里 Configure 方法 + public void Configure(IServiceProvider serviceProvider) + { + var shardingBootstrapper = serviceProvider.GetService(); + shardingBootstrapper.Start(); + // 有一些测试数据要初始化可以放在这里 + InitData(serviceProvider).GetAwaiter().GetResult(); + } + + /// + /// 添加种子数据 + /// + /// + /// + private async Task InitData(IServiceProvider serviceProvider) + { + using (var scope = serviceProvider.CreateScope()) + { + var virtualDbContext = scope.ServiceProvider.GetService(); + if (!await virtualDbContext.Set().ShardingAnyAsync(o => true)) + { + var ids = Enumerable.Range(1, 9000000); + var userMods = new List(); + foreach (var id in ids) + { + userMods.Add(new SysUserMod() + { + Id = id.ToString(), + Age = id, + Name = $"name_{id}", + AgeGroup=Math.Abs(id%10) + }); + + } + + var shardingBatchInsertEntry = virtualDbContext.BulkInsert(userMods); + shardingBatchInsertEntry.BatchGroups.ForEach(g => + { + g.Key.BulkInsert(g.Value); + }); + + await virtualDbContext.SaveChangesAsync(); + } + } + } + } +} \ No newline at end of file