修复仅分库的对象查询bug

This commit is contained in:
xuejiaming 2022-12-09 21:02:52 +08:00
parent 1f810f9442
commit ba42a43e65
6 changed files with 54 additions and 8 deletions

View File

@ -73,6 +73,17 @@ namespace Sample.MySql.Controllers
[HttpGet]
public async Task<IActionResult> Get()
{
var x2 = await (from ut in _defaultTableDbContext.Set<SysTest>()
join uu in _defaultTableDbContext.Set<SysUserLogByMonth>()
on ut.Id equals uu.Id
select new { a = ut, b = uu }).Select(o=>new {x=o}).Select(o=>new{x=o})
.Select(o => new
{
o.x.x.a.Id
}).OrderBy(o => o.Id).ToListAsync();
Console.WriteLine("123");
//OtherDbContext.CurrentId = "";
// var myUsers0 = _otherDbContext.MyUsers.ToList();
// OtherDbContext.CurrentId = "123";
@ -92,6 +103,7 @@ namespace Sample.MySql.Controllers
// .Where(o => o.Id == "2").FirstOrDefaultAsync();
// _defaultTableDbContext.Update(resultX123);
// _defaultTableDbContext.SaveChanges();
Stopwatch sp = Stopwatch.StartNew();
var sysUserMods = await _defaultTableDbContext.Set<SysUserMod>().ToListAsync();
sp.Stop();
@ -199,5 +211,15 @@ namespace Sample.MySql.Controllers
// var sysUserMods2 = await _defaultTableDbContext.Set<SysTest>().FromSqlRaw("select * from SysTest where id='2'").ToListAsync();
return Ok();
}
[HttpGet]
public async Task<IActionResult> Get3()
{
var sysUserMods = await _defaultTableDbContext.Set<SysUserMod>().FirstOrDefaultAsync();
_defaultTableDbContext.SysUserMod.Remove(sysUserMods);
_defaultTableDbContext.SaveChanges();
// 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

@ -2,6 +2,7 @@
using System.Linq.Expressions;
using System.Reflection;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Sample.MySql.Domain.Entities;
using Sample.MySql.Domain.Maps;
@ -14,6 +15,7 @@ namespace Sample.MySql.DbContexts
public class DefaultShardingDbContext : AbstractShardingDbContext, IShardingTableDbContext
{
public DbSet<DynamicTable> DynamicTables { get; set; }
public DbSet<SysUserMod> SysUserMod { get; set; }
public DefaultShardingDbContext(DbContextOptions<DefaultShardingDbContext> options) : base(options)
{
//切记不要在构造函数中使用会让模型提前创建的方法
@ -68,5 +70,6 @@ namespace Sample.MySql.DbContexts
}
public IRouteTail RouteTail { get; set; }
}
}

View File

@ -59,12 +59,22 @@ namespace Sample.MySql
// services.AddHostedService<AutoStart>();
services.AddControllers();
services.AddSingleton<IMemoryCache>(sp => new MemoryCache(new MemoryCacheOptions { SizeLimit = 102400 }));
//
// Action<IServiceProvider, DbContextOptionsBuilder> optionsBuilder = null;
// services.AddDbContext<DefaultShardingDbContext>(optionsBuilder);
// services.AddDbContext<DefaultShardingDbContext>((sp,builder) =>
// {
// optionsBuilder(sp, builder);
// });
//
services.AddShardingDbContext<DefaultShardingDbContext>()
.UseRouteConfig(o =>
{
o.AddShardingTableRoute<DynamicTableRoute>();
o.AddShardingTableRoute<SysUserLogByMonthRoute>();
o.AddShardingTableRoute<SysUserModVirtualTableRoute>();
// o.AddShardingTableRoute<SysUserModVirtualTableRoute>();
o.AddShardingDataSourceRoute<SysUserModVirtualDataSourceRoute>();
}).UseConfig((sp,o) =>
{
@ -73,6 +83,7 @@ namespace Sample.MySql
{
b.UseMemoryCache(memoryCache);
});
o.UseEntityFrameworkCoreProxies = true;
o.CacheModelLockConcurrencyLevel = 1024;
o.CacheEntrySize = 1;
o.CacheModelLockObjectSeconds = 10;
@ -82,12 +93,13 @@ namespace Sample.MySql
// o.UseEntityFrameworkCoreProxies = true;
o.ThrowIfQueryRouteNotMatch = false;
o.AutoUseWriteConnectionStringAfterWriteDb = true;
o.UseShardingQuery((conStr, builder) =>
{
builder.UseMySql(conStr, new MySqlServerVersion(new Version()));
// .UseLoggerFactory(efLogger)
// .EnableSensitiveDataLogging()
//.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);
// .UseLoggerFactory(efLogger)
// .EnableSensitiveDataLogging()
//.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);
});
o.UseShardingTransaction((connection, builder) =>
{
@ -98,11 +110,11 @@ namespace Sample.MySql
//.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);
});
o.AddDefaultDataSource("ds0",
"server=127.0.0.1;port=3306;database=dbdbd0;userid=root;password=root;");
"server=127.0.0.1;port=3306;database=dbdbd02;userid=root;password=root;");
o.AddExtraDataSource(sp => new Dictionary<string, string>()
{
{ "ds1", "server=127.0.0.1;port=3306;database=dbdbd1;userid=root;password=root;" },
{ "ds2", "server=127.0.0.1;port=3306;database=dbdbd2;userid=root;password=root;" }
{ "ds1", "server=127.0.0.1;port=3306;database=dbdbd12;userid=root;password=root;" },
{ "ds2", "server=127.0.0.1;port=3306;database=dbdbd22;userid=root;password=root;" }
});
o.UseShardingMigrationConfigure(b =>
{

View File

@ -146,7 +146,8 @@ namespace Sample.SqlServer.Controllers
var unionUserIds = await _defaultTableDbContext.Set<SysUserMod>().Select(o => new UnionUserId() { UserId = o.Id })
.Union(_defaultTableDbContext.Set<SysUserSalary>().Select(o => new UnionUserId() { UserId = o.UserId })).UseUnionAllMerge().ToListAsync();
.Union(_defaultTableDbContext.Set<SysUserSalary>().Select(o => new UnionUserId() { UserId = o.UserId }))
.UseUnionAllMerge().ToListAsync();

View File

@ -54,6 +54,7 @@ namespace ShardingCore.Core.VirtualRoutes.TableRoutes.RoutingRuleEngine
var queryEntities = tableRouteRuleContext.QueryEntities;
bool onlyShardingDataSource = true;
foreach (var shardingEntityKv in queryEntities)
{
var shardingEntity = shardingEntityKv.Key;
@ -62,6 +63,8 @@ namespace ShardingCore.Core.VirtualRoutes.TableRoutes.RoutingRuleEngine
continue;
}
onlyShardingDataSource = false;
var shardingRouteUnits = GetEntityRouteUnit(tableRouteRuleContext.DataSourceRouteResult, shardingEntity,
shardingEntityKv.Value ?? tableRouteRuleContext.Queryable);
@ -130,6 +133,10 @@ namespace ShardingCore.Core.VirtualRoutes.TableRoutes.RoutingRuleEngine
sqlRouteUnits.Add(new SqlRouteUnit(dataSourceName, tableRouteResult));
}
}
}else if (onlyShardingDataSource)
{
var tableRouteResult = new TableRouteResult(queryEntities.Keys.Select(o=>new TableRouteUnit(dataSourceName,String.Empty,o )).ToList());
sqlRouteUnits.Add(new SqlRouteUnit(dataSourceName, tableRouteResult));
}
}

View File

@ -56,6 +56,7 @@ namespace ShardingCore.Test
})
.UseConfig(op =>
{
op.AutoUseWriteConnectionStringAfterWriteDb = true;
op.CacheModelLockConcurrencyLevel = Environment.ProcessorCount;
//op.UseEntityFrameworkCoreProxies = true;
//当无法获取路由时会返回默认值而不是报错