添加shardingcore吧版本使用说明

This commit is contained in:
xuejiaming 2022-11-17 14:28:08 +08:00
parent 26b1916e09
commit c9aaa32eb3
4 changed files with 41 additions and 31 deletions

View File

@ -50,6 +50,10 @@ Release | EF Core | .NET (Core)
[6.7.0.0+](https://www.nuget.org/packages/ShardingCore)| 2.x | netcoreapp2
## 如何选择版本
- 如果你的efcore版本小于等于6,那么就是使用shardingcore 6.x.x.x的最新版本
- 如果你的efcore版本等于7,那么就是使用shardingcore 7.x.x.x的最新版本
## 快速开始
5步实现按月分表,且支持自动化建表建库
### 第一步安装依赖

View File

@ -48,6 +48,10 @@ Release | EF Core | .NET (Core)
[6.7.0.0+](https://www.nuget.org/packages/ShardingCore)| 3.x | netcoreapp3 or netstandard2.0
[6.7.0.0+](https://www.nuget.org/packages/ShardingCore)| 2.x | netcoreapp2
## How Choose Version
- if u efcore version <=6 use shardingcore 6.x.x.x last version
- if u efcore version ==6 use shardingcore 7.x.x.x last version
## Quick start
5 steps implement sharding by month and support auto create table by month
### Step 1: Install the package

View File

@ -171,20 +171,18 @@ namespace Sample.MySql.Controllers
[HttpGet]
public async Task<IActionResult> Get2()
{
// var sql= from a in _defaultTableDbContext.Set<DynamicTable>()
// join b in _defaultTableDbContext.Set<SysTest>()
// on a.Id equals b.Id into t1
// from aa1 in t1.DefaultIfEmpty()
// // join bc in _defaultTableDbContext.Set<SysTest>()
// // on a.Id equals bc.Id into t2
// // from aa2 in t2.DefaultIfEmpty()
// select new
// {
// ID = a.Id
// };
// var listAsync =await sql.ToListAsync();
var sysUserMods1 = await _defaultTableDbContext.Set<SysUserMod>().FromSqlRaw("select * from SysUserMod where id='2'").ToListAsync();
var sysUserMods2 = await _defaultTableDbContext.Set<SysTest>().FromSqlRaw("select * from SysTest where id='2'").ToListAsync();
var dateTime = new DateTime(2021,1,1);
var sql= from a in _defaultTableDbContext.Set<SysUserLogByMonth>().Where(o=>o.Time==dateTime)
join b in _defaultTableDbContext.Set<SysTest>()
on a.Id equals b.Id into t1
from aa1 in t1.DefaultIfEmpty()
select new
{
ID = a.Id
};
var listAsync =await sql.ToListAsync();
// var sysUserMods1 = await _defaultTableDbContext.Set<SysUserMod>().FromSqlRaw("select * from SysUserMod where id='2'").ToListAsync();
// var sysUserMods2 = await _defaultTableDbContext.Set<SysTest>().FromSqlRaw("select * from SysTest where id='2'").ToListAsync();
return Ok();
}
}

View File

@ -22,30 +22,31 @@ namespace ShardingCore.Core.VirtualRoutes.TableRoutes.RoutingRuleEngine
private readonly ITableRouteManager _tableRouteManager;
private readonly IEntityMetadataManager _entityMetadataManager;
public TableRouteRuleEngine(ITableRouteManager tableRouteManager,
public TableRouteRuleEngine(ITableRouteManager tableRouteManager,
IEntityMetadataManager entityMetadataManager)
{
_tableRouteManager = tableRouteManager;
_entityMetadataManager = entityMetadataManager;
}
private List<TableRouteUnit> GetEntityRouteUnit(DataSourceRouteResult dataSourceRouteResult,Type shardingEntity,IQueryable queryable)
private List<TableRouteUnit> GetEntityRouteUnit(DataSourceRouteResult dataSourceRouteResult,
Type shardingEntity, IQueryable queryable)
{
// if (!_entityMetadataManager.IsShardingTable(shardingEntity))
// {
// // var dataSourceNames = dataSourceRouteResult.IntersectDataSources;
// // var tableRouteUnits = new List<TableRouteUnit>(dataSourceNames.Count);
// // foreach (var dataSourceName in dataSourceNames)
// // {
// // var shardingRouteUnit = new TableRouteUnit(dataSourceName, string.Empty, shardingEntity);
// // tableRouteUnits.Add(shardingRouteUnit);
// // }
// // return tableRouteUnits;
// return new List<TableRouteUnit>(0);
// var dataSourceNames = dataSourceRouteResult.IntersectDataSources;
// var tableRouteUnits = new List<TableRouteUnit>(dataSourceNames.Count);
// foreach (var dataSourceName in dataSourceNames)
// {
// var shardingRouteUnit = new TableRouteUnit(dataSourceName, string.Empty, shardingEntity);
// tableRouteUnits.Add(shardingRouteUnit);
// }
// return tableRouteUnits;
// }
var virtualTableRoute = _tableRouteManager.GetRoute(shardingEntity);
return virtualTableRoute.RouteWithPredicate(dataSourceRouteResult, queryable, true);
}
public ShardingRouteResult Route(TableRouteRuleContext tableRouteRuleContext)
{
Dictionary<string /*dataSourceName*/, Dictionary<Type /*entityType*/, ISet<TableRouteUnit>>> routeMaps =
@ -60,8 +61,10 @@ namespace ShardingCore.Core.VirtualRoutes.TableRoutes.RoutingRuleEngine
{
continue;
}
var shardingRouteUnits = GetEntityRouteUnit(tableRouteRuleContext.DataSourceRouteResult,shardingEntity, shardingEntityKv.Value ?? tableRouteRuleContext.Queryable);
var shardingRouteUnits = GetEntityRouteUnit(tableRouteRuleContext.DataSourceRouteResult, shardingEntity,
shardingEntityKv.Value ?? tableRouteRuleContext.Queryable);
foreach (var shardingRouteUnit in shardingRouteUnits)
{
var dataSourceName = shardingRouteUnit.DataSourceName;
@ -93,7 +96,7 @@ namespace ShardingCore.Core.VirtualRoutes.TableRoutes.RoutingRuleEngine
//=>
//[ds0,[{01,a},{01,b}]],[ds0,[{01,a},{03,b}]],[ds0,[{02,a},{01,b}]],[ds0,[{02,a},{03,b}]],[ds1,[{01,a},{01,b}]]
//如果笛卡尔积
var sqlRouteUnits = new List<ISqlRouteUnit>(31);
int dataSourceCount = 0;
bool isCrossTable = false;
@ -104,7 +107,7 @@ namespace ShardingCore.Core.VirtualRoutes.TableRoutes.RoutingRuleEngine
{
var routeMap = routeMaps[dataSourceName];
var tableRouteResults = routeMap.Select(o => o.Value).Cartesian()
.Select(o => new TableRouteResult(o.ToList())).Where(o=>!o.IsEmpty).ToList();
.Select(o => new TableRouteResult(o.ToList())).Where(o => !o.IsEmpty).ToList();
if (tableRouteResults.IsNotEmpty())
{
dataSourceCount++;
@ -112,6 +115,7 @@ namespace ShardingCore.Core.VirtualRoutes.TableRoutes.RoutingRuleEngine
{
isCrossTable = true;
}
foreach (var tableRouteResult in tableRouteResults)
{
if (tableRouteResult.ReplaceTables.Count > 1)
@ -122,13 +126,14 @@ namespace ShardingCore.Core.VirtualRoutes.TableRoutes.RoutingRuleEngine
existCrossTableTails = true;
}
}
sqlRouteUnits.Add(new SqlRouteUnit(dataSourceName, tableRouteResult));
}
}
}
}
return new ShardingRouteResult(sqlRouteUnits, sqlRouteUnits.Count == 0, dataSourceCount > 1, isCrossTable,
return new ShardingRouteResult(sqlRouteUnits, sqlRouteUnits.Count == 0, dataSourceCount > 1, isCrossTable,
existCrossTableTails);
//
// var sqlRouteUnits = tableRouteRuleContext.DataSourceRouteResult.IntersectDataSources.SelectMany(
@ -142,6 +147,5 @@ namespace ShardingCore.Core.VirtualRoutes.TableRoutes.RoutingRuleEngine
// return sqlRouteUnits;
// return routeMaps.Select(o => o.Value).Cartesian().Where(o=>o).Select(o => new TableRouteResult(o,_shardingDatabaseProvider.GetShardingDbContextType()));
}
}
}