From b5aa66f37a36d80e109649baa0f2a18e3653856a Mon Sep 17 00:00:00 2001 From: xuejiaming <326308290@qq.com> Date: Wed, 6 Jul 2022 22:50:16 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=91=E5=B8=83x.6.0.3=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E8=BF=81=E7=A7=BB=E7=9A=84=E4=BD=BF=E7=94=A8=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nuget-publish.bat | 8 ++-- .../Controllers/WeatherForecastController.cs | 41 ++++++++++++++----- samples/Sample.MySql/Startup.cs | 13 ++---- .../Extensions/ShardingRuntimeExtension.cs | 2 +- src/ShardingCore/ShardingCoreExtension.cs | 10 +++++ 5 files changed, 50 insertions(+), 24 deletions(-) diff --git a/nuget-publish.bat b/nuget-publish.bat index 027ccda0..5770fde0 100644 --- a/nuget-publish.bat +++ b/nuget-publish.bat @@ -1,9 +1,9 @@ :start ::定义版本 -set EFCORE2=2.6.0.2 -set EFCORE3=3.6.0.2 -set EFCORE5=5.6.0.2 -set EFCORE6=6.6.0.2 +set EFCORE2=2.6.0.3 +set EFCORE3=3.6.0.3 +set EFCORE5=5.6.0.3 +set EFCORE6=6.6.0.3 ::删除所有bin与obj下的文件 @echo off diff --git a/samples/Sample.MySql/Controllers/WeatherForecastController.cs b/samples/Sample.MySql/Controllers/WeatherForecastController.cs index 9bc095c3..d5819d6d 100644 --- a/samples/Sample.MySql/Controllers/WeatherForecastController.cs +++ b/samples/Sample.MySql/Controllers/WeatherForecastController.cs @@ -2,9 +2,11 @@ using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; +using System.Data; using System.Linq; using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Storage; using Sample.MySql.DbContexts; using Sample.MySql.Domain.Entities; using ShardingCore.TableCreator; @@ -26,16 +28,35 @@ namespace Sample.MySql.Controllers [HttpGet] public async Task Get() { - var resultX = await _defaultTableDbContext.Set().Where(o => o.Id == "2" || o.Id == "3").FirstOrDefaultAsync(); - var resultY = await _defaultTableDbContext.Set().FirstOrDefaultAsync(o => o.Id == "2" || o.Id == "3"); - var result = await _defaultTableDbContext.Set().AnyAsync(); - var result1 = await _defaultTableDbContext.Set().Where(o => o.Id == "2" || o.Id == "3").ToListAsync(); - var result2 = await _defaultTableDbContext.Set().Skip(1).Take(10).ToListAsync(); - var shardingFirstOrDefaultAsync = await _defaultTableDbContext.Set().ToListAsync(); - var shardingCountAsync = await _defaultTableDbContext.Set().CountAsync(); - var shardingCountAsyn2c = _defaultTableDbContext.Set().Count(); - - return Ok(result1); + // using (var tran = _defaultTableDbContext.Database.BeginTransaction()) + // { + + var resultX = await _defaultTableDbContext.Set().Where(o => o.Id == "2" || o.Id == "3").FirstOrDefaultAsync(); + var resultY = await _defaultTableDbContext.Set().FirstOrDefaultAsync(o => o.Id == "2" || o.Id == "3"); + var result = await _defaultTableDbContext.Set().AnyAsync(); + var result1 = await _defaultTableDbContext.Set().Where(o => o.Id == "2" || o.Id == "3").ToListAsync(); + var result2 = await _defaultTableDbContext.Set().Skip(1).Take(10).ToListAsync(); + var shardingFirstOrDefaultAsync = await _defaultTableDbContext.Set().ToListAsync(); + var shardingCountAsync = await _defaultTableDbContext.Set().CountAsync(); + var shardingCountAsyn2c = _defaultTableDbContext.Set().Count(); + // var dbConnection = _defaultTableDbContext.Database.GetDbConnection(); + // if (dbConnection.State != ConnectionState.Open) + // { + // dbConnection.Open(); + // } + // using (var dbCommand = dbConnection.CreateCommand()) + // { + // dbCommand.CommandText = "select * from systest"; + // dbCommand.Transaction = _defaultTableDbContext.Database.CurrentTransaction?.GetDbTransaction(); + // var dbDataReader = dbCommand.ExecuteReader(); + // while (dbDataReader.Read()) + // { + // Console.WriteLine(dbDataReader[0]); + // } + // } + // } + + return Ok(1); } // [HttpGet] // public async Task Get1() diff --git a/samples/Sample.MySql/Startup.cs b/samples/Sample.MySql/Startup.cs index 790e8bb9..cf303654 100644 --- a/samples/Sample.MySql/Startup.cs +++ b/samples/Sample.MySql/Startup.cs @@ -93,11 +93,6 @@ namespace Sample.MySql {"ds1", "server=127.0.0.1;port=3306;database=dbdbd1;userid=root;password=root;"}, {"ds2", "server=127.0.0.1;port=3306;database=dbdbd2;userid=root;password=root;"} }); - o.UseShellDbContextConfigure(b => - { - b.ReplaceService(); - b.ReplaceService(); - }); o.UseShardingMigrationConfigure(b => { b.ReplaceService(); @@ -164,10 +159,10 @@ namespace Sample.MySql } } - // Stopwatch sp = Stopwatch.StartNew(); - // app.ApplicationServices.UseAutoTryCompensateTable(); - // sp.Stop(); - // Console.WriteLine("UseAutoTryCompensateTable:"+sp.ElapsedMilliseconds); + Stopwatch sp = Stopwatch.StartNew(); + app.ApplicationServices.UseAutoTryCompensateTable(); + sp.Stop(); + Console.WriteLine("UseAutoTryCompensateTable:"+sp.ElapsedMilliseconds); app.UseRouting(); app.UseAuthorization(); diff --git a/src/ShardingCore/Extensions/ShardingRuntimeExtension.cs b/src/ShardingCore/Extensions/ShardingRuntimeExtension.cs index 23b61852..43c891a1 100644 --- a/src/ShardingCore/Extensions/ShardingRuntimeExtension.cs +++ b/src/ShardingCore/Extensions/ShardingRuntimeExtension.cs @@ -25,7 +25,7 @@ namespace ShardingCore.Extensions var compensateTableParallelCount = parallelCount ?? shardingConfigOptions.CompensateTableParallelCount; if (compensateTableParallelCount <= 0) { - throw new ShardingCoreInvalidOperationException($"compensate table parallel count must >0"); + throw new ShardingCoreInvalidOperationException($"compensate table parallel count must > 0"); } var allDataSourceNames = virtualDataSource.GetAllDataSourceNames(); var partitionMigrationUnits = allDataSourceNames.Partition(compensateTableParallelCount); diff --git a/src/ShardingCore/ShardingCoreExtension.cs b/src/ShardingCore/ShardingCoreExtension.cs index e3fb953a..cee49429 100644 --- a/src/ShardingCore/ShardingCoreExtension.cs +++ b/src/ShardingCore/ShardingCoreExtension.cs @@ -27,6 +27,7 @@ using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore.Migrations; using ShardingCore.Bootstrappers; using ShardingCore.Core.DbContextCreator; using ShardingCore.Core.QueryTrackers; @@ -102,10 +103,13 @@ namespace ShardingCore DbContextOptionsBuilder dbContextOptionsBuilder) where TShardingDbContext : DbContext, IShardingDbContext { var shardingRuntimeContext = serviceProvider.GetRequiredService(); + var shardingConfigOptions = shardingRuntimeContext.GetShardingConfigOptions(); + shardingConfigOptions.ShardingMigrationConfigure?.Invoke(dbContextOptionsBuilder); var virtualDataSource = shardingRuntimeContext.GetVirtualDataSource(); var connectionString = virtualDataSource.GetConnectionString(virtualDataSource.DefaultDataSourceName); var contextOptionsBuilder = virtualDataSource.ConfigurationParams .UseDbContextOptionsBuilder(connectionString, dbContextOptionsBuilder) + .UseShardingMigrator() .UseSharding(shardingRuntimeContext); virtualDataSource.ConfigurationParams.UseShellDbContextOptionBuilder(contextOptionsBuilder); @@ -198,6 +202,12 @@ namespace ShardingCore .ReplaceService>(); } + public static DbContextOptionsBuilder UseShardingMigrator( + this DbContextOptionsBuilder optionsBuilder) + { + return optionsBuilder + .ReplaceService(); + } public static DbContextOptionsBuilder UseShardingOptions(this DbContextOptionsBuilder optionsBuilder, IShardingRuntimeContext shardingRuntimeContext)