修复仅分库的对象查询bug

This commit is contained in:
xuejiaming 2022-08-17 11:29:34 +08:00
parent 94b99ffce9
commit 9d80a3bb90
4 changed files with 35 additions and 12 deletions

View File

@ -1,9 +1,9 @@
:start
::定义版本
set EFCORE2=2.6.0.29
set EFCORE3=3.6.0.29
set EFCORE5=5.6.0.29
set EFCORE6=6.6.0.29
set EFCORE2=2.6.0.30
set EFCORE3=3.6.0.30
set EFCORE5=5.6.0.30
set EFCORE6=6.6.0.30
::删除所有bin与obj下的文件
@echo off

View File

@ -80,7 +80,7 @@ namespace Sample.SqlServerShardingDataSource
{
endpoints.MapControllers();
});
//app.InitSeed();
app.InitSeed();
}
}
}

View File

@ -6,16 +6,18 @@ using Sample.SqlServerShardingDataSource.Entities;
using ShardingCore.Core.EntityMetadatas;
using ShardingCore.Core.VirtualRoutes;
using ShardingCore.Core.VirtualRoutes.DataSourceRoutes.Abstractions;
using ShardingCore.Helpers;
namespace Sample.SqlServerShardingDataSource.VirtualRoutes
{
public class OrderVirtualDataSourceRoute : AbstractShardingOperatorVirtualDataSourceRoute<Order, string>
{
private readonly List<string> _dataSources = Enumerable.Range(0,100).Select(o=>(o % 100).ToString().PadLeft(2,'0')).ToList();
private readonly List<string> _dataSources = Enumerable.Range(0,4).Select(o=>(o % 100).ToString().PadLeft(2,'0')).ToList();
//我们设置区域就是数据库
public override string ShardingKeyToDataSourceName(object shardingKey)
{
return shardingKey?.ToString() ?? string.Empty;
var shardingKeyToDataSourceName = Math.Abs(ShardingCoreHelper.GetStringHashCode(shardingKey?.ToString() ?? string.Empty) % 4).ToString().PadLeft(2, '0');
return shardingKeyToDataSourceName;
}
public override List<string> GetAllDataSourceNames()
@ -47,7 +49,7 @@ namespace Sample.SqlServerShardingDataSource.VirtualRoutes
public override void Configure(EntityMetadataDataSourceBuilder<Order> builder)
{
builder.ShardingProperty(o => o.Area);
builder.ShardingProperty(o => o.Id);
}
}
}

View File

@ -39,7 +39,32 @@ namespace ShardingCore.Core.VirtualRoutes.TableRoutes.RoutingRuleEngine
{
var shardingEntity = shardingEntityKv.Key;
if (!_entityMetadataManager.IsShardingTable(shardingEntity))
{
var dataSourceNames = tableRouteRuleContext.DataSourceRouteResult.IntersectDataSources;
foreach (var dataSourceName in dataSourceNames)
{
var shardingRouteUnit = new TableRouteUnit(dataSourceName, string.Empty, shardingEntity);
if (!routeMaps.ContainsKey(dataSourceName))
{
routeMaps.Add(dataSourceName,
new Dictionary<Type, ISet<TableRouteUnit>>()
{ { shardingEntity, new HashSet<TableRouteUnit>() { shardingRouteUnit } } });
}
else
{
var routeMap = routeMaps[dataSourceName];
if (!routeMap.ContainsKey(shardingEntity))
{
routeMap.Add(shardingEntity, new HashSet<TableRouteUnit>() { shardingRouteUnit });
}
else
{
routeMap[shardingEntity].Add(shardingRouteUnit);
}
}
}
continue;
}
var virtualTableRoute = _tableRouteManager.GetRoute(shardingEntity);
var shardingRouteUnits = virtualTableRoute.RouteWithPredicate(
tableRouteRuleContext.DataSourceRouteResult,
@ -107,10 +132,6 @@ namespace ShardingCore.Core.VirtualRoutes.TableRoutes.RoutingRuleEngine
}
}
}
else
{
routeMaps.Add(dataSourceName,new Dictionary<Type, ISet<TableRouteUnit>>());
}
}
return new ShardingRouteResult(sqlRouteUnits, sqlRouteUnits.Count == 0, dataSourceCount > 1, isCrossTable,