diff --git a/samples/Sample.MySql/Controllers/WeatherForecastController.cs b/samples/Sample.MySql/Controllers/WeatherForecastController.cs index 96ef1d49..c7643e90 100644 --- a/samples/Sample.MySql/Controllers/WeatherForecastController.cs +++ b/samples/Sample.MySql/Controllers/WeatherForecastController.cs @@ -168,5 +168,23 @@ namespace Sample.MySql.Controllers Console.WriteLine("-----------------------------------------------------------------------------------------------------"); return Ok(); } + [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 sysUserMods = await _defaultTableDbContext.Set().FromSqlRaw("select * from SysUserMod where id='2'").ToListAsync(); + return Ok(); + } } } diff --git a/samples/Sample.MySql/DbContexts/DefaultShardingDbContext.cs b/samples/Sample.MySql/DbContexts/DefaultShardingDbContext.cs index c53e8aed..931d0dc2 100644 --- a/samples/Sample.MySql/DbContexts/DefaultShardingDbContext.cs +++ b/samples/Sample.MySql/DbContexts/DefaultShardingDbContext.cs @@ -13,6 +13,7 @@ namespace Sample.MySql.DbContexts { public class DefaultShardingDbContext : AbstractShardingDbContext, IShardingTableDbContext { + public DbSet DynamicTables { get; set; } public DefaultShardingDbContext(DbContextOptions options) : base(options) { //切记不要在构造函数中使用会让模型提前创建的方法 diff --git a/samples/Sample.MySql/Domain/Entities/DynamicTable.cs b/samples/Sample.MySql/Domain/Entities/DynamicTable.cs new file mode 100644 index 00000000..0ce0056f --- /dev/null +++ b/samples/Sample.MySql/Domain/Entities/DynamicTable.cs @@ -0,0 +1,9 @@ +using System.ComponentModel.DataAnnotations.Schema; + +namespace Sample.MySql.Domain.Entities; + +[Table("DynamicTable")] +public class DynamicTable +{ + public string Id { get; set; } +} \ No newline at end of file diff --git a/samples/Sample.MySql/Shardings/DynamicTableRoute.cs b/samples/Sample.MySql/Shardings/DynamicTableRoute.cs new file mode 100644 index 00000000..8f88798f --- /dev/null +++ b/samples/Sample.MySql/Shardings/DynamicTableRoute.cs @@ -0,0 +1,34 @@ +using Sample.MySql.Domain.Entities; +using ShardingCore.Core.EntityMetadatas; +using ShardingCore.Core.VirtualRoutes; +using ShardingCore.Core.VirtualRoutes.TableRoutes.Abstractions; + +namespace Sample.MySql.Shardings; + +public class DynamicTableRoute:AbstractShardingOperatorVirtualTableRoute +{ + public override string ShardingKeyToTail(object shardingKey) + { + return $"{shardingKey}"; + } + + public override List GetTails() + { + return new List(); + } + + public override void Configure(EntityMetadataTableBuilder builder) + { + builder.ShardingProperty(o => o.Id); + } + + public override Func GetRouteToFilter(string shardingKey, ShardingOperatorEnum shardingOperator) + { + var shardingKeyToTail = ShardingKeyToTail(shardingKey); + switch (shardingOperator) + { + case ShardingOperatorEnum.Equal: return t => t == shardingKeyToTail; + default: return t => true; + } + } +} \ No newline at end of file diff --git a/samples/Sample.MySql/Startup.cs b/samples/Sample.MySql/Startup.cs index e69c994f..accc4c96 100644 --- a/samples/Sample.MySql/Startup.cs +++ b/samples/Sample.MySql/Startup.cs @@ -98,11 +98,14 @@ namespace Sample.MySql services.AddShardingDbContext() .UseRouteConfig(o => { + o.AddShardingTableRoute(); o.AddShardingTableRoute(); o.AddShardingTableRoute(); o.AddShardingDataSourceRoute(); - }).UseConfig(o => + }).UseConfig((sp,o) => { + var loggerFactory1= sp.GetService(); + var loggerFactory2 = sp.ApplicationServiceProvider.GetService(); // o.UseEntityFrameworkCoreProxies = true; o.ThrowIfQueryRouteNotMatch = false; o.AutoUseWriteConnectionStringAfterWriteDb = true; diff --git a/samples/Sample.MySql/appsettings.Development.json b/samples/Sample.MySql/appsettings.Development.json index 63ed6e14..1d7a4e1a 100644 --- a/samples/Sample.MySql/appsettings.Development.json +++ b/samples/Sample.MySql/appsettings.Development.json @@ -1,7 +1,7 @@ { "Logging": { "LogLevel": { - "Default": "Information", + "Default": "Debug", "Microsoft": "Information", "Microsoft.Hosting.Lifetime": "Information" } diff --git a/src/ShardingCore/Core/VirtualRoutes/TableRoutes/RoutingRuleEngine/TableRouteRuleEngine.cs b/src/ShardingCore/Core/VirtualRoutes/TableRoutes/RoutingRuleEngine/TableRouteRuleEngine.cs index 657becb3..4eec3c67 100644 --- a/src/ShardingCore/Core/VirtualRoutes/TableRoutes/RoutingRuleEngine/TableRouteRuleEngine.cs +++ b/src/ShardingCore/Core/VirtualRoutes/TableRoutes/RoutingRuleEngine/TableRouteRuleEngine.cs @@ -31,17 +31,18 @@ namespace ShardingCore.Core.VirtualRoutes.TableRoutes.RoutingRuleEngine 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; - } + // 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 virtualTableRoute = _tableRouteManager.GetRoute(shardingEntity); return virtualTableRoute.RouteWithPredicate(dataSourceRouteResult, queryable, true); } @@ -55,6 +56,10 @@ namespace ShardingCore.Core.VirtualRoutes.TableRoutes.RoutingRuleEngine foreach (var shardingEntityKv in queryEntities) { var shardingEntity = shardingEntityKv.Key; + if (!_entityMetadataManager.IsShardingTable(shardingEntity)) + { + continue; + } var shardingRouteUnits = GetEntityRouteUnit(tableRouteRuleContext.DataSourceRouteResult,shardingEntity, shardingEntityKv.Value ?? tableRouteRuleContext.Queryable); foreach (var shardingRouteUnit in shardingRouteUnits)