This commit is contained in:
xuejiaming 2022-08-17 11:08:27 +08:00
parent 8d5f0a5b4a
commit ad9b273d08
2 changed files with 5 additions and 4 deletions

View File

@ -55,7 +55,7 @@ namespace Sample.SqlServerShardingDataSource
"Data Source=localhost;Initial Catalog=EFCoreShardingDataSourceOnly00;Integrated Security=True;");
op.AddExtraDataSource(sp =>
{
return Enumerable.Range(1, 100).Select(o => (o % 100).ToString().PadLeft(2, '0')).ToList()
return Enumerable.Range(1, 3).Select(o => o.ToString().PadLeft(2, '0')).ToList()
.ToDictionary(o => o,
o =>
$"Data Source=localhost;Initial Catalog=EFCoreShardingDataSourceOnly{o};Integrated Security=True;");

View File

@ -6,6 +6,7 @@ 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
{
@ -13,12 +14,12 @@ namespace Sample.SqlServerShardingDataSource.VirtualRoutes
{
private readonly List<string> _dataSources = new List<string>()
{
"A", "B", "C"
"00", "01", "02","03"
};
//我们设置区域就是数据库
public override string ShardingKeyToDataSourceName(object shardingKey)
{
return shardingKey?.ToString() ?? string.Empty;
return Math.Abs(ShardingCoreHelper.GetStringHashCode(shardingKey?.ToString() ?? string.Empty) % 4).ToString().PadLeft(2, '0');
}
public override List<string> GetAllDataSourceNames()
@ -50,7 +51,7 @@ namespace Sample.SqlServerShardingDataSource.VirtualRoutes
public override void Configure(EntityMetadataDataSourceBuilder<SysUser> builder)
{
builder.ShardingProperty(o => o.Area);
builder.ShardingProperty(o => o.Id);
}
}
}