From 26d559bba7b77db77c59b3edb7be6cbe126c4b95 Mon Sep 17 00:00:00 2001 From: xuejiaming <326308290@qq.com> Date: Mon, 22 Mar 2021 14:43:36 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20efcore2.x=20=E6=97=A0?= =?UTF-8?q?=E6=B3=95=E9=87=8D=E6=96=B0=E7=BC=96=E8=AF=91=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nuget-publish.bat | 6 +++--- .../Controllers/WeatherForecastController.cs | 3 +++ samples/Sample.MySql/DIExtension.cs | 11 ++++++++++ .../DbContexts/DefaultTableDbContext.cs | 2 ++ .../Domain/Entities/SysUserLogByMonth.cs | 15 ++++++++++++++ .../Domain/Maps/SysUserLogByMonthMap.cs | 20 +++++++++++++++++++ .../Shardings/SysUserLogByMonthRoute.cs | 17 ++++++++++++++++ samples/Sample.MySql/Startup.cs | 6 +++++- samples/Sample.MySql/appsettings.json | 2 +- .../EFCores/ShardingMySqlQuerySqlGenerator.cs | 4 ++-- .../GenericInMemoryMergeEngine.cs | 8 +++++--- .../EFCores/ShardingQueryCompiler.cs | 8 ++++++++ 12 files changed, 92 insertions(+), 10 deletions(-) create mode 100644 samples/Sample.MySql/Domain/Entities/SysUserLogByMonth.cs create mode 100644 samples/Sample.MySql/Domain/Maps/SysUserLogByMonthMap.cs create mode 100644 samples/Sample.MySql/Shardings/SysUserLogByMonthRoute.cs diff --git a/nuget-publish.bat b/nuget-publish.bat index e91089bc..623c0880 100644 --- a/nuget-publish.bat +++ b/nuget-publish.bat @@ -1,8 +1,8 @@ :start ::定义版本 -set EFCORE2=2.1.0.5 -set EFCORE3=3.1.0.5 -set EFCORE5=5.1.0.5 +set EFCORE2=2.1.0.6 +set EFCORE3=3.1.0.6 +set EFCORE5=5.1.0.6 ::删除所有bin与obj下的文件 @echo off diff --git a/samples/Sample.MySql/Controllers/WeatherForecastController.cs b/samples/Sample.MySql/Controllers/WeatherForecastController.cs index 59dffd1d..180eb37a 100644 --- a/samples/Sample.MySql/Controllers/WeatherForecastController.cs +++ b/samples/Sample.MySql/Controllers/WeatherForecastController.cs @@ -28,6 +28,9 @@ namespace Sample.MySql.Controllers { var result = await _virtualDbContext.Set().AnyAsync(); var result1 = await _virtualDbContext.Set().Where(o => o.Id == "2" || o.Id == "3").ToShardingListAsync(); + var shardingCountAsync = await _virtualDbContext.Set().ShardingCountAsync(); + var shardingCountAsyn2c = await _virtualDbContext.Set().ShardingCountAsync(); + return Ok(result1); } } diff --git a/samples/Sample.MySql/DIExtension.cs b/samples/Sample.MySql/DIExtension.cs index 435f5467..5ac591ce 100644 --- a/samples/Sample.MySql/DIExtension.cs +++ b/samples/Sample.MySql/DIExtension.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using System.Linq; using Microsoft.AspNetCore.Builder; @@ -42,8 +43,18 @@ namespace Sample.MySql Name = $"name_{id}", }); } + var userModMonths = new List(); + foreach (var id in ids) + { + userModMonths.Add(new SysUserLogByMonth() + { + Id = id.ToString(), + Time = DateTime.Now + }); + } virtualDbContext.InsertRange(userMods); + virtualDbContext.InsertRange(userModMonths); virtualDbContext.SaveChanges(); } } diff --git a/samples/Sample.MySql/DbContexts/DefaultTableDbContext.cs b/samples/Sample.MySql/DbContexts/DefaultTableDbContext.cs index 55ce47e7..5251d146 100644 --- a/samples/Sample.MySql/DbContexts/DefaultTableDbContext.cs +++ b/samples/Sample.MySql/DbContexts/DefaultTableDbContext.cs @@ -11,11 +11,13 @@ namespace Sample.MySql.DbContexts } + protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.ApplyConfiguration(new SysUserModMap()); modelBuilder.ApplyConfiguration(new SysTestMap()); + modelBuilder.ApplyConfiguration(new SysUserLogByMonthMap()); } public string ModelChangeKey { get; set; } diff --git a/samples/Sample.MySql/Domain/Entities/SysUserLogByMonth.cs b/samples/Sample.MySql/Domain/Entities/SysUserLogByMonth.cs new file mode 100644 index 00000000..673d316a --- /dev/null +++ b/samples/Sample.MySql/Domain/Entities/SysUserLogByMonth.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using ShardingCore.Core; + +namespace Sample.MySql.Domain.Entities +{ + public class SysUserLogByMonth : IShardingTable + { + public string Id { get; set; } + [ShardingTableKey] + public DateTime Time { get; set; } + } +} diff --git a/samples/Sample.MySql/Domain/Maps/SysUserLogByMonthMap.cs b/samples/Sample.MySql/Domain/Maps/SysUserLogByMonthMap.cs new file mode 100644 index 00000000..cedca253 --- /dev/null +++ b/samples/Sample.MySql/Domain/Maps/SysUserLogByMonthMap.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Metadata.Builders; +using Sample.MySql.Domain.Entities; + +namespace Sample.MySql.Domain.Maps +{ + public class SysUserLogByMonthMap : IEntityTypeConfiguration + { + public void Configure(EntityTypeBuilder builder) + { + builder.HasKey(o => o.Id); + builder.Property(o => o.Id).IsRequired().HasMaxLength(128); + builder.ToTable(nameof(SysUserLogByMonth)); + } + } +} \ No newline at end of file diff --git a/samples/Sample.MySql/Shardings/SysUserLogByMonthRoute.cs b/samples/Sample.MySql/Shardings/SysUserLogByMonthRoute.cs new file mode 100644 index 00000000..49b5f883 --- /dev/null +++ b/samples/Sample.MySql/Shardings/SysUserLogByMonthRoute.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Sample.MySql.Domain.Entities; +using ShardingCore.VirtualRoutes.Months; + +namespace Sample.MySql.Shardings +{ + public class SysUserLogByMonthRoute:AbstractSimpleShardingMonthKeyDateTimeVirtualTableRoute + { + public override DateTime GetBeginTime() + { + return new DateTime(2021, 3, 01); + } + } +} diff --git a/samples/Sample.MySql/Startup.cs b/samples/Sample.MySql/Startup.cs index 9cc1f968..8152fae1 100644 --- a/samples/Sample.MySql/Startup.cs +++ b/samples/Sample.MySql/Startup.cs @@ -9,6 +9,7 @@ using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore; using Sample.MySql.DbContexts; using Sample.MySql.Shardings; using ShardingCore.MySql; @@ -32,13 +33,16 @@ namespace Sample.MySql { o.EnsureCreatedWithOutShardingTable = true; o.CreateShardingTableOnStart = true; - o.AddShardingDbContextWithShardingTable("conn1", "server=xxxx;userid=xxxx;password=xxxx;database=sharding_db123;Charset=utf8;Allow Zero Datetime=True; Pooling=true; Max Pool Size=512;sslmode=none;Allow User Variables=True;", dbConfig => + o.AddShardingDbContextWithShardingTable("conn1", "server=xxx;userid=xxx;password=xxx;database=sharding_db123;Charset=utf8;Allow Zero Datetime=True; Pooling=true; Max Pool Size=512;sslmode=none;Allow User Variables=True;", dbConfig => { dbConfig.AddShardingTableRoute(); + dbConfig.AddShardingTableRoute(); }); //o.AddDataSourceVirtualRoute<>(); }); + + services.AddLogging(b => b.AddConsole()); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. diff --git a/samples/Sample.MySql/appsettings.json b/samples/Sample.MySql/appsettings.json index d9d9a9bf..214d63f9 100644 --- a/samples/Sample.MySql/appsettings.json +++ b/samples/Sample.MySql/appsettings.json @@ -2,7 +2,7 @@ "Logging": { "LogLevel": { "Default": "Information", - "Microsoft": "Warning", + "Microsoft": "Information", "Microsoft.Hosting.Lifetime": "Information" } }, diff --git a/src/ShardingCore.MySql/EFCores/ShardingMySqlQuerySqlGenerator.cs b/src/ShardingCore.MySql/EFCores/ShardingMySqlQuerySqlGenerator.cs index fd5bfa0c..4edc67e2 100644 --- a/src/ShardingCore.MySql/EFCores/ShardingMySqlQuerySqlGenerator.cs +++ b/src/ShardingCore.MySql/EFCores/ShardingMySqlQuerySqlGenerator.cs @@ -1,4 +1,5 @@ #if !EFCORE2 +using System; using System.Linq; using System.Linq.Expressions; using Microsoft.EntityFrameworkCore.Query; @@ -89,7 +90,6 @@ namespace ShardingCore.MySql.EFCores { newTableName = "(" + string.Join(" union all ", tails.Select(tail => $"select * from {sqlGenerationHelper.DelimitIdentifier($"{tableExpression.Name}{tailPrefix}{tail}", tableExpression.Schema)}")) + ")"; } - var relationalCommandBuilder = typeof(QuerySqlGenerator).GetTypeFieldValue(this, "_relationalCommandBuilder") as IRelationalCommandBuilder; relationalCommandBuilder.Append(newTableName).Append(this.AliasSeparator).Append(sqlGenerationHelper.DelimitIdentifier(tableExpression.Alias)); return tableExpression; @@ -105,6 +105,7 @@ namespace ShardingCore.MySql.EFCores #endif #if EFCORE2 +using System; using System.Linq; using System.Linq.Expressions; using ShardingCore; @@ -204,7 +205,6 @@ namespace ShardingCore.MySql.EFCores return tableExpression; } } - var result = base.VisitTable(tableExpression); return result; } diff --git a/src/ShardingCore/Core/Internal/StreamMerge/GenericMerges/GenericInMemoryMergeEngine.cs b/src/ShardingCore/Core/Internal/StreamMerge/GenericMerges/GenericInMemoryMergeEngine.cs index 203db715..e5875016 100644 --- a/src/ShardingCore/Core/Internal/StreamMerge/GenericMerges/GenericInMemoryMergeEngine.cs +++ b/src/ShardingCore/Core/Internal/StreamMerge/GenericMerges/GenericInMemoryMergeEngine.cs @@ -44,10 +44,12 @@ namespace ShardingCore.Core.Internal.StreamMerge.GenericMerges ICollection parallelDbContexts = new LinkedList(); try { + var intersectConfigs = _mergeContext.GetDataSourceRoutingResult().IntersectConfigs; - var enumeratorTasks = _mergeContext.GetDataSourceRoutingResult().IntersectConfigs.SelectMany(connectKey => + var enumeratorTasks = intersectConfigs.SelectMany(connectKey => { - return _mergeContext.GetRouteResults(connectKey).Select(routeResult => + var routeResults = _mergeContext.GetRouteResults(connectKey); + return routeResults.Select(routeResult => { return Task.Run(async () => { @@ -58,7 +60,7 @@ namespace ShardingCore.Core.Internal.StreamMerge.GenericMerges return await EFCoreExecute(connectKey,newQueryable, routeResult, efQuery); }); - }); + }).ToList(); }).ToArray(); return (await Task.WhenAll(enumeratorTasks)).ToList(); diff --git a/src/ShardingCore/EFCores/ShardingQueryCompiler.cs b/src/ShardingCore/EFCores/ShardingQueryCompiler.cs index 608466e0..9418b9c7 100644 --- a/src/ShardingCore/EFCores/ShardingQueryCompiler.cs +++ b/src/ShardingCore/EFCores/ShardingQueryCompiler.cs @@ -165,6 +165,7 @@ namespace ShardingCore.EFCores var compiledQuery = CompileQueryCore(query, _queryModelGenerator, _database, _logger, _contextType); + return compiledQuery(queryContext); } @@ -190,6 +191,13 @@ namespace ShardingCore.EFCores public override Task ExecuteAsync(Expression query, CancellationToken cancellationToken) { + + var shardingAccessor = ShardingContainer.Services.GetService(); + if (shardingAccessor?.ShardingContext != null) + { + return ShardingExecuteAsync(query, cancellationToken); + } + return base.ExecuteAsync(query, cancellationToken); } private Task ShardingExecuteAsync(Expression query, CancellationToken cancellationToken)