添加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 [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步实现按月分表,且支持自动化建表建库 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)| 3.x | netcoreapp3 or netstandard2.0
[6.7.0.0+](https://www.nuget.org/packages/ShardingCore)| 2.x | netcoreapp2 [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 ## Quick start
5 steps implement sharding by month and support auto create table by month 5 steps implement sharding by month and support auto create table by month
### Step 1: Install the package ### Step 1: Install the package

View File

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

View File

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