修复dynamicshardinghelper的创建库的bug导致默认对象也会创建 发布x.6.0.7

This commit is contained in:
xuejiaming 2022-07-12 10:29:31 +08:00
parent 3699ccf426
commit 36b844da19
4 changed files with 33 additions and 32 deletions

View File

@ -13,7 +13,7 @@ namespace ShardingCore6x
public void Configure(EntityTypeBuilder<Order> 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);

View File

@ -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;

View File

@ -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<string, EntityType>;
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]);

View File

@ -124,7 +124,7 @@ namespace ShardingCore.Sharding.Enumerators.AggregateExtensions
return callExpression;
}
/// <summary>
/// 根据属性求和
/// 根据属性求和
/// </summary>
/// <param name="source"></param>
/// <param name="property"></param>
@ -141,7 +141,7 @@ namespace ShardingCore.Sharding.Enumerators.AggregateExtensions
return source.Provider.Execute<TSelect>(callExpression);
}
/// <summary>
/// 根据属性求和
/// 根据属性求和
/// </summary>
/// <param name="source"></param>
/// <param name="propertyName"></param>
@ -156,7 +156,7 @@ namespace ShardingCore.Sharding.Enumerators.AggregateExtensions
return source.SumByProperty(property);
}
/// <summary>
/// 对
/// 对
/// </summary>
/// <typeparam name="TSelect"></typeparam>
/// <param name="source"></param>
@ -290,12 +290,12 @@ namespace ShardingCore.Sharding.Enumerators.AggregateExtensions
return source.Provider.Execute(callExpression);
}
/// <summary>
/// 获取平均数和 [{avg1,count1},{avg2,count2}....]=>sum(avg1...n*count1...n)/sum(count1...n)
/// 获取平均数和 [{avg1,count1},{avg2,count2}....]=>sum(avg1...n*count1...n)/sum(count1...n)
/// </summary>
/// <param name="source">数据源</param>
/// <param name="averagePropertyName">聚合函数average属性名</param>
/// <param name="countPropertyName">聚合函数count属性名</param>
/// <param name="resultType">平均值返回结果:int/int=double</param>
/// <param name="source">数据源</param>
/// <param name="averagePropertyName">聚合函数average属性名</param>
/// <param name="countPropertyName">聚合函数count属性名</param>
/// <param name="resultType">平均值返回结果:int/int=double</param>
[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
//}
/// <summary>
/// 获取平均数和 [{avg1,sum1},{avg2,sum2}....]=>sum(sum1...n)/sum(sum1...n/avg1...n)
/// 获取平均数和 [{avg1,sum1},{avg2,sum2}....]=>sum(sum1...n)/sum(sum1...n/avg1...n)
/// </summary>
/// <param name="source">数据源</param>
/// <param name="averagePropertyName">聚合函数average属性名</param>
/// <param name="sumPropertyName">聚合函数sum属性名</param>
/// <param name="resultType">平均值返回结果:int/int=double</param>
/// <param name="source">数据源</param>
/// <param name="averagePropertyName">聚合函数average属性名</param>
/// <param name="sumPropertyName">聚合函数sum属性名</param>
/// <param name="resultType">平均值返回结果:int/int=double</param>
[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);
}
/// <summary>
/// 获取平均数和 [{avg1,count1},{avg2,count2}....]=>sum(avg1...n*count1...n)/sum(count1...n)
/// 获取平均数和 [{avg1,count1},{avg2,count2}....]=>sum(avg1...n*count1...n)/sum(count1...n)
/// </summary>
/// <param name="source">数据源</param>
/// <param name="averageProperty">聚合函数average属性名</param>
/// <param name="countProperty">聚合函数count属性名</param>
/// <param name="source">数据源</param>
/// <param name="averageProperty">聚合函数average属性名</param>
/// <param name="countProperty">聚合函数count属性名</param>
/// <returns></returns>
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
}
/// <summary>
/// 获取平均数个数 [{avg1,sum1},{avg2,sum2}....]=>sum(sum1..n)/sum(sum1...n/avg1...n)
/// 获取平均数个数 [{avg1,sum1},{avg2,sum2}....]=>sum(sum1..n)/sum(sum1...n/avg1...n)
/// </summary>
/// <param name="source">数据源</param>
/// <param name="averageProperty">聚合函数average属性名</param>
/// <param name="sumProperty">聚合函数count属性名</param>
/// <param name="source">数据源</param>
/// <param name="averageProperty">聚合函数average属性名</param>
/// <param name="sumProperty">聚合函数count属性名</param>
/// <returns></returns>
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);