修复当分片表无路由的时候join普通表报错的bug

This commit is contained in:
xuejiaming 2022-11-16 19:57:29 +08:00
parent 674350b60d
commit 97974b7f2c
7 changed files with 83 additions and 13 deletions

View File

@ -168,5 +168,23 @@ namespace Sample.MySql.Controllers
Console.WriteLine("-----------------------------------------------------------------------------------------------------");
return Ok();
}
[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 sysUserMods = await _defaultTableDbContext.Set<SysUserMod>().FromSqlRaw("select * from SysUserMod where id='2'").ToListAsync();
return Ok();
}
}
}

View File

@ -13,6 +13,7 @@ namespace Sample.MySql.DbContexts
{
public class DefaultShardingDbContext : AbstractShardingDbContext, IShardingTableDbContext
{
public DbSet<DynamicTable> DynamicTables { get; set; }
public DefaultShardingDbContext(DbContextOptions<DefaultShardingDbContext> options) : base(options)
{
//切记不要在构造函数中使用会让模型提前创建的方法

View File

@ -0,0 +1,9 @@
using System.ComponentModel.DataAnnotations.Schema;
namespace Sample.MySql.Domain.Entities;
[Table("DynamicTable")]
public class DynamicTable
{
public string Id { get; set; }
}

View File

@ -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<DynamicTable,string>
{
public override string ShardingKeyToTail(object shardingKey)
{
return $"{shardingKey}";
}
public override List<string> GetTails()
{
return new List<string>();
}
public override void Configure(EntityMetadataTableBuilder<DynamicTable> builder)
{
builder.ShardingProperty(o => o.Id);
}
public override Func<string, bool> GetRouteToFilter(string shardingKey, ShardingOperatorEnum shardingOperator)
{
var shardingKeyToTail = ShardingKeyToTail(shardingKey);
switch (shardingOperator)
{
case ShardingOperatorEnum.Equal: return t => t == shardingKeyToTail;
default: return t => true;
}
}
}

View File

@ -98,11 +98,14 @@ namespace Sample.MySql
services.AddShardingDbContext<DefaultShardingDbContext>()
.UseRouteConfig(o =>
{
o.AddShardingTableRoute<DynamicTableRoute>();
o.AddShardingTableRoute<SysUserLogByMonthRoute>();
o.AddShardingTableRoute<SysUserModVirtualTableRoute>();
o.AddShardingDataSourceRoute<SysUserModVirtualDataSourceRoute>();
}).UseConfig(o =>
}).UseConfig((sp,o) =>
{
var loggerFactory1= sp.GetService<ILoggerFactory>();
var loggerFactory2 = sp.ApplicationServiceProvider.GetService<ILoggerFactory>();
// o.UseEntityFrameworkCoreProxies = true;
o.ThrowIfQueryRouteNotMatch = false;
o.AutoUseWriteConnectionStringAfterWriteDb = true;

View File

@ -1,7 +1,7 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Default": "Debug",
"Microsoft": "Information",
"Microsoft.Hosting.Lifetime": "Information"
}

View File

@ -31,17 +31,18 @@ namespace ShardingCore.Core.VirtualRoutes.TableRoutes.RoutingRuleEngine
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;
}
// 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 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)