diff --git a/README-zh.md b/README-zh.md index dc3a8a9f..a26fa51c 100644 --- a/README-zh.md +++ b/README-zh.md @@ -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步实现按月分表,且支持自动化建表建库 ### 第一步安装依赖 diff --git a/README.md b/README.md index 4dfce2c5..a23c3bc4 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/samples/Sample.MySql/Controllers/WeatherForecastController.cs b/samples/Sample.MySql/Controllers/WeatherForecastController.cs index 5cb5e449..20badec5 100644 --- a/samples/Sample.MySql/Controllers/WeatherForecastController.cs +++ b/samples/Sample.MySql/Controllers/WeatherForecastController.cs @@ -171,20 +171,18 @@ namespace Sample.MySql.Controllers [HttpGet] public async Task Get2() { - // var sql= from a in _defaultTableDbContext.Set() - // join b in _defaultTableDbContext.Set() - // on a.Id equals b.Id into t1 - // from aa1 in t1.DefaultIfEmpty() - // // join bc in _defaultTableDbContext.Set() - // // 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().FromSqlRaw("select * from SysUserMod where id='2'").ToListAsync(); - var sysUserMods2 = await _defaultTableDbContext.Set().FromSqlRaw("select * from SysTest where id='2'").ToListAsync(); + var dateTime = new DateTime(2021,1,1); + var sql= from a in _defaultTableDbContext.Set().Where(o=>o.Time==dateTime) + join b in _defaultTableDbContext.Set() + 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().FromSqlRaw("select * from SysUserMod where id='2'").ToListAsync(); + // var sysUserMods2 = await _defaultTableDbContext.Set().FromSqlRaw("select * from SysTest where id='2'").ToListAsync(); return Ok(); } } diff --git a/src/ShardingCore/Core/VirtualRoutes/TableRoutes/RoutingRuleEngine/TableRouteRuleEngine.cs b/src/ShardingCore/Core/VirtualRoutes/TableRoutes/RoutingRuleEngine/TableRouteRuleEngine.cs index 4eec3c67..0a95b547 100644 --- a/src/ShardingCore/Core/VirtualRoutes/TableRoutes/RoutingRuleEngine/TableRouteRuleEngine.cs +++ b/src/ShardingCore/Core/VirtualRoutes/TableRoutes/RoutingRuleEngine/TableRouteRuleEngine.cs @@ -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 GetEntityRouteUnit(DataSourceRouteResult dataSourceRouteResult,Type shardingEntity,IQueryable queryable) + private List GetEntityRouteUnit(DataSourceRouteResult dataSourceRouteResult, + Type shardingEntity, IQueryable queryable) { // if (!_entityMetadataManager.IsShardingTable(shardingEntity)) // { - // // var dataSourceNames = dataSourceRouteResult.IntersectDataSources; - // // var tableRouteUnits = new List(dataSourceNames.Count); - // // foreach (var dataSourceName in dataSourceNames) - // // { - // // var shardingRouteUnit = new TableRouteUnit(dataSourceName, string.Empty, shardingEntity); - // // tableRouteUnits.Add(shardingRouteUnit); - // // } - // // return tableRouteUnits; - // return new List(0); + // var dataSourceNames = dataSourceRouteResult.IntersectDataSources; + // var tableRouteUnits = new List(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>> 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(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())); } - } } \ No newline at end of file