From 36b844da190ae42d8f7c9579dfab24c0f03770e2 Mon Sep 17 00:00:00 2001 From: xuejiaming <326308290@qq.com> Date: Tue, 12 Jul 2022 10:29:31 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Ddynamicshardinghelper?= =?UTF-8?q?=E7=9A=84=E5=88=9B=E5=BB=BA=E5=BA=93=E7=9A=84bug=E5=AF=BC?= =?UTF-8?q?=E8=87=B4=E9=BB=98=E8=AE=A4=E5=AF=B9=E8=B1=A1=E4=B9=9F=E4=BC=9A?= =?UTF-8?q?=E5=88=9B=E5=BB=BA=20=E5=8F=91=E5=B8=83x.6.0.7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- benchmarks/ShardingCoreBenchmark/OrderMap.cs | 2 +- samples/Sample.MySql/Startup.cs | 1 + .../Extensions/DbContextExtension.cs | 6 +- .../AggregateExtensions/AggregateExtension.cs | 56 +++++++++---------- 4 files changed, 33 insertions(+), 32 deletions(-) diff --git a/benchmarks/ShardingCoreBenchmark/OrderMap.cs b/benchmarks/ShardingCoreBenchmark/OrderMap.cs index 0d109d6e..88f5aeb9 100644 --- a/benchmarks/ShardingCoreBenchmark/OrderMap.cs +++ b/benchmarks/ShardingCoreBenchmark/OrderMap.cs @@ -13,7 +13,7 @@ namespace ShardingCore6x public void Configure(EntityTypeBuilder builder) { builder.HasKey(o => o.Id); - builder.Property(o => o.Id).IsUnicode(false).HasMaxLength(50); + builder.Property(o => o.Id).ValueGeneratedOnAdd().IsUnicode(false).HasMaxLength(50); builder.Property(o => o.Body).IsRequired().HasDefaultValue(string.Empty).HasMaxLength(128); builder.Property(o => o.Remark).IsRequired().HasDefaultValue(string.Empty).HasMaxLength(128); builder.Property(o => o.Payer).IsRequired().IsUnicode(false).HasMaxLength(50); diff --git a/samples/Sample.MySql/Startup.cs b/samples/Sample.MySql/Startup.cs index 461a757c..0867d11f 100644 --- a/samples/Sample.MySql/Startup.cs +++ b/samples/Sample.MySql/Startup.cs @@ -9,6 +9,7 @@ using ShardingCore.Core; using ShardingCore.Core.RuntimeContexts; using ShardingCore.EFCores; using ShardingCore.Extensions; +using ShardingCore.Helpers; using ShardingCore.TableExists; using ShardingCore.TableExists.Abstractions; diff --git a/src/ShardingCore/Extensions/DbContextExtension.cs b/src/ShardingCore/Extensions/DbContextExtension.cs index 22be81b6..e1e986d2 100644 --- a/src/ShardingCore/Extensions/DbContextExtension.cs +++ b/src/ShardingCore/Extensions/DbContextExtension.cs @@ -125,7 +125,7 @@ namespace ShardingCore.Extensions #if EFCORE6 var contextModelRelationalModel = contextModel.GetRelationalModel() as RelationalModel; var valueTuples = - contextModelRelationalModel.Tables.Where(o => o.Value.EntityTypeMappings.Any(m => entityMetadataManager.IsShardingTable(m.EntityType.ClrType) ||entityMetadataManager.TryGet(m.EntityType.ClrType)==null)).Select(o => o.Key).ToList(); + contextModelRelationalModel.Tables.Where(o => o.Value.EntityTypeMappings.Any(m => !entityMetadataManager.IsShardingDataSource(m.EntityType.ClrType) ||entityMetadataManager.TryGet(m.EntityType.ClrType)==null)).Select(o => o.Key).ToList(); for (int i = 0; i < valueTuples.Count; i++) { contextModelRelationalModel.Tables.Remove(valueTuples[i]); @@ -134,7 +134,7 @@ namespace ShardingCore.Extensions #if EFCORE5 var contextModelRelationalModel = contextModel.RelationalModel as RelationalModel; var valueTuples = - contextModelRelationalModel.Tables.Where(o => o.Value.EntityTypeMappings.Any(m => entityMetadataManager.IsShardingTable(m.EntityType.ClrType)||entityMetadataManager.TryGet(m.EntityType.ClrType)==null)).Select(o => o.Key).ToList(); + contextModelRelationalModel.Tables.Where(o => o.Value.EntityTypeMappings.Any(m => !entityMetadataManager.IsShardingDataSource(m.EntityType.ClrType)||entityMetadataManager.TryGet(m.EntityType.ClrType)==null)).Select(o => o.Key).ToList(); for (int i = 0; i < valueTuples.Count; i++) { contextModelRelationalModel.Tables.Remove(valueTuples[i]); @@ -143,7 +143,7 @@ namespace ShardingCore.Extensions #if EFCORE2 || EFCORE3 var entityTypes = contextModel.GetFieldValue("_entityTypes") as SortedDictionary; - var list = entityTypes.Where(o => entityMetadataManager.IsShardingTable(o.Value.ClrType) || entityMetadataManager.TryGet(o.Value.ClrType) == null).Select(o => o.Key).ToList(); + var list = entityTypes.Where(o => !entityMetadataManager.IsShardingDataSource(o.Value.ClrType) || entityMetadataManager.TryGet(o.Value.ClrType) == null).Select(o => o.Key).ToList(); for (int i = 0; i < list.Count; i++) { entityTypes.Remove(list[i]); diff --git a/src/ShardingCore/Sharding/Enumerators/AggregateExtensions/AggregateExtension.cs b/src/ShardingCore/Sharding/Enumerators/AggregateExtensions/AggregateExtension.cs index 30bd3544..d3d6efba 100644 --- a/src/ShardingCore/Sharding/Enumerators/AggregateExtensions/AggregateExtension.cs +++ b/src/ShardingCore/Sharding/Enumerators/AggregateExtensions/AggregateExtension.cs @@ -124,7 +124,7 @@ namespace ShardingCore.Sharding.Enumerators.AggregateExtensions return callExpression; } /// - /// 根据属性求和 + /// 鏍规嵁灞炴ф眰鍜 /// /// /// @@ -141,7 +141,7 @@ namespace ShardingCore.Sharding.Enumerators.AggregateExtensions return source.Provider.Execute(callExpression); } /// - /// 根据属性求和 + /// 鏍规嵁灞炴ф眰鍜 /// /// /// @@ -156,7 +156,7 @@ namespace ShardingCore.Sharding.Enumerators.AggregateExtensions return source.SumByProperty(property); } /// - /// 对 + /// 瀵 /// /// /// @@ -290,12 +290,12 @@ namespace ShardingCore.Sharding.Enumerators.AggregateExtensions return source.Provider.Execute(callExpression); } /// - /// 获取平均数和 [{avg1,count1},{avg2,count2}....]=>sum(avg1...n*count1...n)/sum(count1...n) + /// 鑾峰彇骞冲潎鏁板拰 [{avg1,count1},{avg2,count2}....]=>sum(avg1...n*count1...n)/sum(count1...n) /// - /// 数据源 - /// 聚合函数average属性名 - /// 聚合函数count属性名 - /// 平均值返回结果:int/int=double + /// 鏁版嵁婧 + /// 鑱氬悎鍑芥暟average灞炴у悕 + /// 鑱氬悎鍑芥暟count灞炴у悕 + /// 骞冲潎鍊艰繑鍥炵粨鏋:int/int=double [ExcludeFromCodeCoverage] public static object AverageWithCount(this IQueryable source, string averagePropertyName, string countPropertyName, Type resultType) { @@ -311,7 +311,7 @@ namespace ShardingCore.Sharding.Enumerators.AggregateExtensions if (source == null) throw new ArgumentNullException(nameof(source)); if (averageProperty == null) throw new ArgumentNullException(nameof(averageProperty)); if (countProperty == null) throw new ArgumentNullException(nameof(countProperty)); - //获取sum + //鑾峰彇sum var sum = source.AverageSum(averageProperty, countProperty); var count = source.SumByProperty(countProperty); return AverageConstant(sum, count,resultType); @@ -327,11 +327,11 @@ namespace ShardingCore.Sharding.Enumerators.AggregateExtensions { Expression constantSum = Expression.Constant(sum); - //如果计算类型和返回类型不一致先转成一致 + //濡傛灉璁$畻绫诲瀷鍜岃繑鍥炵被鍨嬩笉涓鑷村厛杞垚涓鑷 if(sum.GetType()!=resultType) constantSum = Expression.Convert(constantSum, resultType); Expression constantCount = Expression.Constant(count); - //如果计算类型和返回类型不一致先转成一致 + //濡傛灉璁$畻绫诲瀷鍜岃繑鍥炵被鍨嬩笉涓鑷村厛杞垚涓鑷 if (count.GetType() != resultType) constantCount = Expression.Convert(constantCount, resultType); var binaryExpression = Expression.Divide(constantSum, constantCount); @@ -342,11 +342,11 @@ namespace ShardingCore.Sharding.Enumerators.AggregateExtensions { var resultType = typeof(TResult); Expression constantSum = Expression.Constant(sum); - //如果计算类型和返回类型不一致先转成一致 + //濡傛灉璁$畻绫诲瀷鍜岃繑鍥炵被鍨嬩笉涓鑷村厛杞垚涓鑷 if (sum.GetType() != resultType) constantSum = Expression.Convert(constantSum, resultType); Expression constantCount = Expression.Constant(count); - //如果计算类型和返回类型不一致先转成一致 + //濡傛灉璁$畻绫诲瀷鍜岃繑鍥炵被鍨嬩笉涓鑷村厛杞垚涓鑷 if (count.GetType() != resultType) constantCount = Expression.Convert(constantCount, resultType); var binaryExpression = Expression.Divide(constantSum, constantCount); @@ -368,12 +368,12 @@ namespace ShardingCore.Sharding.Enumerators.AggregateExtensions //} /// - /// 获取平均数和 [{avg1,sum1},{avg2,sum2}....]=>sum(sum1...n)/sum(sum1...n/avg1...n) + /// 鑾峰彇骞冲潎鏁板拰 [{avg1,sum1},{avg2,sum2}....]=>sum(sum1...n)/sum(sum1...n/avg1...n) /// - /// 数据源 - /// 聚合函数average属性名 - /// 聚合函数sum属性名 - /// 平均值返回结果:int/int=double + /// 鏁版嵁婧 + /// 鑱氬悎鍑芥暟average灞炴у悕 + /// 鑱氬悎鍑芥暟sum灞炴у悕 + /// 骞冲潎鍊艰繑鍥炵粨鏋:int/int=double [ExcludeFromCodeCoverage] public static object AverageWithSum(this IQueryable source, string averagePropertyName, string sumPropertyName, Type resultType) { @@ -394,11 +394,11 @@ namespace ShardingCore.Sharding.Enumerators.AggregateExtensions return AverageConstant(sum, count, resultType); } /// - /// 获取平均数和 [{avg1,count1},{avg2,count2}....]=>sum(avg1...n*count1...n)/sum(count1...n) + /// 鑾峰彇骞冲潎鏁板拰 [{avg1,count1},{avg2,count2}....]=>sum(avg1...n*count1...n)/sum(count1...n) /// - /// 数据源 - /// 聚合函数average属性名 - /// 聚合函数count属性名 + /// 鏁版嵁婧 + /// 鑱氬悎鍑芥暟average灞炴у悕 + /// 鑱氬悎鍑芥暟count灞炴у悕 /// private static object AverageSum(this IQueryable source, PropertyInfo averageProperty, PropertyInfo countProperty) { @@ -411,7 +411,7 @@ namespace ShardingCore.Sharding.Enumerators.AggregateExtensions MemberExpression averageMember = Expression.MakeMemberAccess(parameter, averageProperty); //o.count MemberExpression countMember = Expression.MakeMemberAccess(parameter, countProperty); - //Convert(o.count,o.avg.GetType()) 必须要同类型才能计算 + //Convert(o.count,o.avg.GetType()) 蹇呴』瑕佸悓绫诲瀷鎵嶈兘璁$畻 var countConvertExpression = Expression.Convert(countMember, averageProperty.PropertyType); //o.avg*Convert(o.count,o.avg.GetType()) var multiply = Expression.Multiply(averageMember, countConvertExpression); @@ -434,11 +434,11 @@ namespace ShardingCore.Sharding.Enumerators.AggregateExtensions } /// - /// 获取平均数个数 [{avg1,sum1},{avg2,sum2}....]=>sum(sum1..n)/sum(sum1...n/avg1...n) + /// 鑾峰彇骞冲潎鏁颁釜鏁 [{avg1,sum1},{avg2,sum2}....]=>sum(sum1..n)/sum(sum1...n/avg1...n) /// - /// 数据源 - /// 聚合函数average属性名 - /// 聚合函数count属性名 + /// 鏁版嵁婧 + /// 鑱氬悎鍑芥暟average灞炴у悕 + /// 鑱氬悎鍑芥暟count灞炴у悕 /// private static object AverageCount(this IQueryable source, PropertyInfo averageProperty, PropertyInfo sumProperty) { @@ -451,7 +451,7 @@ namespace ShardingCore.Sharding.Enumerators.AggregateExtensions MemberExpression averageMember = Expression.MakeMemberAccess(parameter, averageProperty); //o.sum MemberExpression sumMember = Expression.MakeMemberAccess(parameter, sumProperty); - //Convert(o.sum,o.avg.GetType()) 必须要同类型才能计算 + //Convert(o.sum,o.avg.GetType()) 蹇呴』瑕佸悓绫诲瀷鎵嶈兘璁$畻 var sumConvertExpression = Expression.Convert(sumMember, averageProperty.PropertyType); //Convert(o.sum,o.avg.GetType())/o.avg var divide = Expression.Divide(sumConvertExpression, averageMember);